PHP,包括其使用类领域从基础文件外部文件
比方说,我有一个类...PHP,包括其使用类领域从基础文件外部文件
class A {
private $action;
private $includes;
public function __construct($action, $file) {
//assign fields
}
public function includeFile()
include_once($this->file);
}
$a = new A('foo.process.php', 'somefile.php');
$a->includeFile();
正如你所看到的,includeFile()调用包括来自内功能,因此一旦外部文件被包含在内,它应该在技术上属于我理解的函数内部。
我做到了这一点后,让我们来看看该文件包括在内,这是somefile.php
,这就要求现场像这样。
<form action=<?=$this->action;?> method="post" name="someForm">
<!--moar markup here-->
</form>
当我尝试这样做时,我收到一个错误。然而,在像Joomla这样的CMS中,我总是看到这一点。这怎么可能?
更新
这是我得到的错误。
Fatal error: Using $this when not in object context in /var/www/form/form.process.php on line 8
更新2
这里是我的代码:
class EditForm implements ISave{
private $formName;
private $adData;
private $photoData;
private $urlData;
private $includes;
public function __construct(AdData $adData, PhotoData $photoData, UrlData $urlData, $includes) {
$this->formName = 'pageOne';
$this->adData = $adData;
$this->photoData = $photoData;
$this->urlData = $urlData;
$this->includes = $includes;
}
public function saveData() {
$this->adData->saveData();
$this->photoData->saveData();
}
public function includeFiles() {
if (is_array($this->includes)) {
foreach($this->includes as $file) {
include_once($file);
}
} else {
include_once($this->includes);
}
}
public function populateCategories($parent) {
$categories = $this->getCategories($parent);
$this->printCategories($categories);
}
public function populateCountries() {
$countries = $this->getCountries();
$this->printCountries($countries);
}
public function populateSubCategories() {
//TODO
}
private function getCategories($parent) {
$db = patentionConnect();
$query =
"SELECT * FROM `jos_adsmanager_categories`
WHERE `parent` = :parent";
$result = $db->fetchAll(
$query,
array(
new PQO(':parent', $parent)
)
);
return $result;
}
private function getCountries() {
$db = patentionConnect();
$query =
"SELECT `fieldtitle` FROM `jos_adsmanager_field_values`
WHERE fieldid = :id";
$result = $db->fetchAll(
$query,
array(
new PQO(':id', 29)
)
);
return $result;
}
private function printCountries(array $countries) {
foreach($countries as $row) {
?>
<option value=<?=$row['fieldtitle'];?> >
<?=$row['fieldtitle'];?>
</option>
<?php
}
}
private function printCategories(array $categories) {
foreach($categories as $key => $row){
?>
<option value=<?=$row['id'];?>>
<?=$row['name'];?>
</option>
<?php
}
}
}
而且包括呼叫(存在于同一个文件):
$template = new EditForm(
new AdData(),
new PhotoData(),
new UrlData($Itemid),
array(
'form.php',
'form.process.php'
)
);
$template->includeFiles();
和主文件其中包括...
if ($this->formName == "pageOne") {
$this->adData->addData('category', $_POST['category']);
$this->adData->addData('subcategory', $_POST['subcategory']);
} else if ($this->formName == "pageTwo") {
$this->adData->addData('ad_Website', $_POST['ad_Website']);
$this->adData->addData('ad_Patent', $_POST['ad_Patent']);
$this->adData->addData('ad_Address', $_POST['ad_Address']);
$this->adData->addData('email', $_POST['email']);
$this->adData->addData('hide_email', $_POST['hide_email']);
$this->adData->addData('ad_phone', $_POST['ad_phone']);
$this->adData->addData('ad_Protection', $_POST['ad_Protection']);
$this->adData->addData('ad_Number', $_POST['ad_Number']);
$this->adData->addData('ad_Country', $_POST['ad_Country']);
$this->adData->addData('ad_issuedate', $_POST['issuedate'] . '/' . $_POST['issuemonth'] . '/' . $_POST['issueyear']);
} else if ($this->formName == "pageThree") {
$this->adData->addData('name', $_POST['name']);
$this->adData->addData('ad_Background', $_POST['ad_Background']);
$this->adData->addData('ad_opeartion', $_POST['ad_operation']);
$this->adData->addData('ad_advlimit', $_POST['ad_advlimit']);
$this->adData->addData('ad_status', $_POST['ad_status']);
$this->adData->addData('ad_addinfo', $_POST['ad_addinfo']);
$this->adData->addData('ad_description', $_POST['ad_description']);
$this->adData->addData('tags', $_POST['tags']);
$this->adData->addData('videolink', $_POST['videolink']);
} else if ($this->formName == "pageFour") {
foreach($_POST['jos_photos'] as $photo) {
$this->photoData->addData(
array(
'title' => $photo['title'],
'url' => $photo['url'],
'ad_id' => $this->photoData->__get('ad_id'),
'userid' => $this->photoData->__get('userid')
)
);
}
}
这应该工作。你确定你正在做一个非静态方法的include
,它是类的一部分(在你的例子中是类A)?你能发布你正在使用的确切代码吗?
编辑:对于这样的问题,一般建议,看看你是否可以裁减代码以使这个问题在很短的,简单的例子,任何人都可以轻松地复制/粘贴复制确切的错误是重复的。大多数时候,你会在试图简化的过程中自己找出答案。如果你不这样做,它会使其他人更容易帮助你进行调试。
在这里,我发布了我的代码。谢谢... – zeboidlund
的'includeFiles()'函数未在代码的任何地方叫... –
对不起,我忘了补充一点;它被称为。我会及时发布它 - 它在类中被称为同一个文件。 – zeboidlund
更新
奇怪:当错误本身一直没相当相关的问题是什么,我发现,这只是我还没有实现一个未定义的领域。
考虑这个线程解决。对于那些回复的人,我当然非常感激。
请发表您的错误...谢谢! – Matt
在这里,你去。谢谢你,先生。 – zeboidlund
OP似乎已经解决了问题(如他们在下面发布的答案中所见)。投票结束这太局部化。 –