[thelist] basic OO php question

Mattias Thorslund mattias at thorslund.us
Wed Apr 18 05:27:32 CDT 2007


I'm not sure why you might want to do this, but I made some comments below.

/Mattias

Rick den Haan wrote:
> Hello,
>
> I feel like a noob asking this, but I've never needed to do this before.
>
> If I call a class (Y) from within another class (X), is it possible to
> access properties of X from within Y without have Y extend X?
>   

Just instantiate the other class as you would anywhere else.

> If that's vague, allow me to give a barebones example of what I want:
>
> <?php
> class X
> {
>     var $foo = "bar";
>
>     function X()
>     {
>         $y = new Y();
>   
//Not sure if this matters to you but $y goes immediately out of scope
and is not used for anything.

>     }
> }
>
> class Y
> {
>     function Y()
>     {
>         echo parent::$foo;
>   
echo X::foo; //not sure if class variables work, but if not, this would:

$oX = new X();
echo $oX->foo;

>         // Doesn't work, because Y does not extend X, but I want this to
> output "bar"
>     }
> }
> ?>
>
> I'd pass $foo along with the call, but in the practical use I have in mind
> X() has a lot more properties. I could potentially pass $this along, but I
> doubt if that's a very recommendable practice.
>
> Any thoughts?
>
> Rick.
>
>   




More information about the thelist mailing list