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

时间:2011-11-25 05:16:43

标签: php include external field

假设我有一个班级......

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')
            )
        );
    }

}

2 个答案:

答案 0 :(得分:1)

这应该有效。你确定你是从一个非静态方法做的include,这个方法属于类(在你的例子中是A类)吗?你能发布你正在使用的确切代码吗?

编辑:作为对这类问题的一般建议,看看你是否可以减少代码,以便在一个简短的例子中重现问题,任何人都可以轻松复制/粘贴以重现确切的错误。大多数时候,你会在尝试简化的过程中自己找出答案。如果不这样做,它将使其他人更容易帮助您进行调试。

答案 1 :(得分:1)

<强>更新

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

考虑这个线程已解决。对于那些回复的人,无论如何我当然很感激。