3

Let's say I have a class...

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();

As you can see, includeFile() calls the include from within the function, therefore once the external file is included, it should technically be inside of the function from my understanding.

After I've done that, let's look at the file included, which is somefile.php, which calls the field like so.

<form action=<?=$this->action;?> method="post" name="someForm">
<!--moar markup here-->
</form>

When I try to do this, I receive an error. Yet, in a CMS like Joomla I see this accomplished all the time. How is this possible?

Update

Here's the error I get.

Fatal error: Using $this when not in object context in /var/www/form/form.process.php on line 8

Update 2

Here's my code:

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       
        }
    }


}

And the include call (which exists in the same file):

$template = new EditForm(
    new AdData(),
    new PhotoData(),
    new UrlData($Itemid),
    array(
        'form.php', 
        'form.process.php'
    )
);

$template->includeFiles();

And the main file which is included...

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
  • 1
    Please post your error... thanks! Commented Nov 25, 2011 at 5:23
  • OP seems to have solved question (as seen in an answer they posted below). Voting to close this as too localized. Commented Nov 25, 2011 at 6:10

2 Answers 2

1

Update

Strange: while the error itself hadn't been quite related to what the problem was, I found that it was simply an undefined field which I hadn't implemented.

Consider this thread solved. To those who replied, I certainly appreciate it regardless.

Sign up to request clarification or add additional context in comments.

Comments

1

This should work. Are you sure you're doing the include from a non-static method that's part of the class (class A in your example)? Can you post the exact code you're using?

Edit: As general advice for problems like this, see if you can trim down the code so the problem is reproducible in a short, simple example that anyone could easily copy/paste to reproduce the exact error. The majority of the time, you'll figure out the answer yourself in the process of trying to simplify. And if you don't, it will make it much easier for others to help you debug.

3 Comments

Sorry, I forgot to add that; it is being called. I'll post it promptly - it's called within the same file as the class.
Is form.php using $this? It seems that file is getting included without any errors, which makes me suspect something is going wrong either in form.php or at the top of form.process.php.
A short, simple, and complete repro case would really help. See my updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.