Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use an instance of a class assigned to a property of another class in the other class in php
    primarykey
    data
    text
    <p>I am making a framework in PHP. I have an import function in library/core.php.</p> <p>I can use the function like this:</p> <pre><code>$core-&gt;import("someclass"); </code></pre> <p>This is the function:</p> <pre><code>public function import() { $import_resources = func_get_args(); $check_directories = array("library", "template", "view", "action", "errors"); $instances = array(); foreach($import_resources as $resource) { for($i = 0; $i &lt;= count($check_directories) - 1; $i++) { if(file_exists($this-&gt;appRoot() . $check_directories[$i] . "/" . $resource . ".php")) { $classes = get_declared_classes(); include ($check_directories[$i] . "/" . $resource . ".php"); $included_classes = array_diff(get_declared_classes(), $classes); $last_class = end($included_classes); $last_class_lowercase = strtolower($last_class); $this-&gt;$last_class_lowercase = new $last_class(); // create an instance of the included class and attach it to the Core Class } else { } } } } </code></pre> <p>So in an other class, I can use it like this:</p> <pre><code>$core-&gt;import("view"); $core-&gt;view-&gt;get(); </code></pre> <p>The whole point of this, was to make the included class available in another class, when it is extended. </p> <pre><code>class Someclass extends Core { public function somefunc() { $this-&gt;view-&gt;get(); // This does not work. } } </code></pre> <p>How could I make it work like this? This is a very important part of the framework, because this is how it works. I think it works similar in popular frameworks like CodeIgniter too. </p> <p>I was trying to use <code>parent::view-&gt;get()</code>, but I guess I don't fully understand it. </p> <p>I hope I can figure this out, because it is holding me down in my work. Thank you in advance.</p>
    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