gpt4 book ai didi

PHP 包括使用基本文件中的类字段的外部文件

转载 作者:搜寻专家 更新时间:2023-10-31 21:16:09 24 4
gpt4 key购买 nike

假设我有一个类...

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() 在 函数中调用 include,因此一旦外部文件被包含,根据我的理解,它在技术上应该在函数内部。

完成后,让我们看一下包含的文件,它是 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')
)
);
}

}

最佳答案

更新

奇怪:虽然错误本身与问题所在完全无关,但我发现它只是一个我没有实现的未定义字段。

考虑此线程已解决。对于那些回复的人,无论如何我都非常感谢。

关于PHP 包括使用基本文件中的类字段的外部文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8265045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com