Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP including external files which use class fields from base file
    primarykey
    data
    text
    <p>Let's say I have a class...</p> <pre><code>class A { private $action; private $includes; public function __construct($action, $file) { //assign fields } public function includeFile() include_once($this-&gt;file); } $a = new A('foo.process.php', 'somefile.php'); $a-&gt;includeFile(); </code></pre> <p>As you can see, includeFile() calls the include from <em>within</em> the function, therefore once the external file is included, it should technically be inside of the function from my understanding.</p> <p>After I've done that, let's look at the file included, which is <code>somefile.php</code>, which calls the field like so.</p> <pre><code>&lt;form action=&lt;?=$this-&gt;action;?&gt; method="post" name="someForm"&gt; &lt;!--moar markup here--&gt; &lt;/form&gt; </code></pre> <p>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?</p> <p><strong>Update</strong></p> <p>Here's the error I get.</p> <pre><code>Fatal error: Using $this when not in object context in /var/www/form/form.process.php on line 8 </code></pre> <p><strong>Update 2</strong></p> <p>Here's my code:</p> <pre><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-&gt;formName = 'pageOne'; $this-&gt;adData = $adData; $this-&gt;photoData = $photoData; $this-&gt;urlData = $urlData; $this-&gt;includes = $includes; } public function saveData() { $this-&gt;adData-&gt;saveData(); $this-&gt;photoData-&gt;saveData(); } public function includeFiles() { if (is_array($this-&gt;includes)) { foreach($this-&gt;includes as $file) { include_once($file); } } else { include_once($this-&gt;includes); } } public function populateCategories($parent) { $categories = $this-&gt;getCategories($parent); $this-&gt;printCategories($categories); } public function populateCountries() { $countries = $this-&gt;getCountries(); $this-&gt;printCountries($countries); } public function populateSubCategories() { //TODO } private function getCategories($parent) { $db = patentionConnect(); $query = "SELECT * FROM `jos_adsmanager_categories` WHERE `parent` = :parent"; $result = $db-&gt;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-&gt;fetchAll( $query, array( new PQO(':id', 29) ) ); return $result; } private function printCountries(array $countries) { foreach($countries as $row) { ?&gt; &lt;option value=&lt;?=$row['fieldtitle'];?&gt; &gt; &lt;?=$row['fieldtitle'];?&gt; &lt;/option&gt; &lt;?php } } private function printCategories(array $categories) { foreach($categories as $key =&gt; $row){ ?&gt; &lt;option value=&lt;?=$row['id'];?&gt;&gt; &lt;?=$row['name'];?&gt; &lt;/option&gt; &lt;?php } } } </code></pre> <p>And the include call (which exists in the same file):</p> <pre><code>$template = new EditForm( new AdData(), new PhotoData(), new UrlData($Itemid), array( 'form.php', 'form.process.php' ) ); $template-&gt;includeFiles(); </code></pre> <p>And the main file which is included...</p> <pre><code>if ($this-&gt;formName == "pageOne") { $this-&gt;adData-&gt;addData('category', $_POST['category']); $this-&gt;adData-&gt;addData('subcategory', $_POST['subcategory']); } else if ($this-&gt;formName == "pageTwo") { $this-&gt;adData-&gt;addData('ad_Website', $_POST['ad_Website']); $this-&gt;adData-&gt;addData('ad_Patent', $_POST['ad_Patent']); $this-&gt;adData-&gt;addData('ad_Address', $_POST['ad_Address']); $this-&gt;adData-&gt;addData('email', $_POST['email']); $this-&gt;adData-&gt;addData('hide_email', $_POST['hide_email']); $this-&gt;adData-&gt;addData('ad_phone', $_POST['ad_phone']); $this-&gt;adData-&gt;addData('ad_Protection', $_POST['ad_Protection']); $this-&gt;adData-&gt;addData('ad_Number', $_POST['ad_Number']); $this-&gt;adData-&gt;addData('ad_Country', $_POST['ad_Country']); $this-&gt;adData-&gt;addData('ad_issuedate', $_POST['issuedate'] . '/' . $_POST['issuemonth'] . '/' . $_POST['issueyear']); } else if ($this-&gt;formName == "pageThree") { $this-&gt;adData-&gt;addData('name', $_POST['name']); $this-&gt;adData-&gt;addData('ad_Background', $_POST['ad_Background']); $this-&gt;adData-&gt;addData('ad_opeartion', $_POST['ad_operation']); $this-&gt;adData-&gt;addData('ad_advlimit', $_POST['ad_advlimit']); $this-&gt;adData-&gt;addData('ad_status', $_POST['ad_status']); $this-&gt;adData-&gt;addData('ad_addinfo', $_POST['ad_addinfo']); $this-&gt;adData-&gt;addData('ad_description', $_POST['ad_description']); $this-&gt;adData-&gt;addData('tags', $_POST['tags']); $this-&gt;adData-&gt;addData('videolink', $_POST['videolink']); } else if ($this-&gt;formName == "pageFour") { foreach($_POST['jos_photos'] as $photo) { $this-&gt;photoData-&gt;addData( array( 'title' =&gt; $photo['title'], 'url' =&gt; $photo['url'], 'ad_id' =&gt; $this-&gt;photoData-&gt;__get('ad_id'), 'userid' =&gt; $this-&gt;photoData-&gt;__get('userid') ) ); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload