Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing a class that has been instantiated by a method within a separate class
    text
    copied!<p>In a nutshell: how do I access the methods of a class that is instantiated by a method of a different class?</p> <p>There is a bit of code I am looking at right now that does the following (altering the code is not an option, btw. It isn't mine... I'm just decipher it):</p> <p>A class has a method that instantiates a different class. It looks something like this:</p> <pre> // this file is named fooClassHere.php class Foo{ public $meh; function bar(){ $something = new Baz; $this->meh = $something->getStuff(); } } </pre> <p>What I am trying to figure out is how to access the methods of this instantiated class Baz. Another page contains something like the following:</p> <pre> include 'bazClassHere.php'; include 'fooClassHere.php'; $a = new Foo; $a->bar(); </pre> <p>So shouldn't all of Baz be available now in some manner (and not just getStuff() which I assigned to $this->meh)? The Foo and Baz classes are included, the code instantiates Foo and then calls Foo's method bar() which in turn instantiates the Baz class. Obviously the following will display data returned by Baz's getStuff() method:</p> <pre> var_dump($a->meh); </pre> <p>But I'd like to access <strong>all</strong> of Baz's available methods without going through the intermediate step of manually assigning them like I did inside Foo's bar method: <pre>$this->meh = $something->getStuff()</pre></p> <p>Maybe something like (but of course this doesn't work):<pre> $a = new Foo;<br> $a->bar(); //instantiates Baz as $something $a->something->fromBaz(); //$something is an instance of Baz, yes? No? </pre></p> <p>I hope this makes sense and I didn't confuse the issue with my notes. Ack! >_&lt;</p>
 

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