From: Walter <php...@to...> - 2010-11-30 01:53:33
|
I have 2 classes, the first instantiates the second... $this->other_class = new Other_Class; Now I would like to the constrictor of Other_Class "know" who called it and pull property data from that class. Say I have this.. $this->x = 6; $this->y = true; How can the constructor of Other_Class pull the values of 'x' and 'y' from the first class? Any ideas? Thanks Walter |
From: David A. <da...@ho...> - 2010-11-30 02:36:35
|
Pass $this to the "Other_Class" via constructor: $this->other_class = new Other_Class($this); Both x and y will need to have public visibility in order for Other_Class to access them directly like that. Otherwise you will need to implement __get() or use specific getters. On Nov 29, 2010, at 6:20 PM, Walter wrote: > I have 2 classes, the first instantiates the second... > > $this->other_class = new Other_Class; > > Now I would like to the constrictor of Other_Class "know" who called > it and pull property data from that class. > > Say I have this.. > > $this->x = 6; > $this->y = true; > > How can the constructor of Other_Class pull the values of 'x' and 'y' > from the first class? > > Any ideas? > > Thanks > > Walter > > ------------------------------------------------------------------------------ > Increase Visibility of Your 3D Game App & Earn a Chance To Win $500! > Tap into the largest installed PC base & get more eyes on your game by > optimizing for Intel(R) Graphics Technology. Get started today with the > Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs. > http://p.sf.net/sfu/intelisp-dev2dev > _______________________________________________ > chiPHPug-discuss mailing list > chi...@li... > https://lists.sourceforge.net/lists/listinfo/chiphpug-discuss -- David Abdemoulaie http://hobodave.com/ http://twitter.com/hobodave |
From: Junkmail <Jun...@2z...> - 2010-11-30 03:34:36
|
I am having a hard time understanding why one would need/want to use this technique. Can you explain a bit about the theory behind this pattern? I don't think I even understand how PHP would handle that. Creating a class and then setting it to another class? "David Abdemoulaie" <da...@ho...> wrote: >Pass $this to the "Other_Class" via constructor: > > $this->other_class = new Other_Class($this); > >Both x and y will need to have public visibility in order for >Other_Class to access them directly like that. Otherwise you will need >to implement __get() or use specific getters. > >On Nov 29, 2010, at 6:20 PM, Walter wrote: > >> I have 2 classes, the first instantiates the second... >> >> $this->other_class = new Other_Class; >> >> Now I would like to the constrictor of Other_Class "know" who called >> it and pull property data from that class. >> >> Say I have this.. >> >> $this->x = 6; >> $this->y = true; >> >> How can the constructor of Other_Class pull the values of 'x' and 'y' >> from the first class? >> >> Any ideas? >> >> Thanks >> >> Walter >> >> >------------------------------------------------------------------------------ >> Increase Visibility of Your 3D Game App & Earn a Chance To Win $500! >> Tap into the largest installed PC base & get more eyes on your game >by >> optimizing for Intel(R) Graphics Technology. Get started today with >the >> Intel(R) Software Partner Program. Five $500 cash prizes are up for >grabs. >> http://p.sf.net/sfu/intelisp-dev2dev >> _______________________________________________ >> chiPHPug-discuss mailing list >> chi...@li... >> https://lists.sourceforge.net/lists/listinfo/chiphpug-discuss > >-- >David Abdemoulaie >http://hobodave.com/ >http://twitter.com/hobodave > > >------------------------------------------------------------------------------ >Increase Visibility of Your 3D Game App & Earn a Chance To Win $500! >Tap into the largest installed PC base & get more eyes on your game by >optimizing for Intel(R) Graphics Technology. Get started today with the >Intel(R) Software Partner Program. Five $500 cash prizes are up for >grabs. >http://p.sf.net/sfu/intelisp-dev2dev >_______________________________________________ >chiPHPug-discuss mailing list >chi...@li... >https://lists.sourceforge.net/lists/listinfo/chiphpug-discuss |
From: Larry G. <la...@ga...> - 2010-11-30 04:02:43
|
You're just setting a property of an object to be another object. That is quite common and totally appropriate in many many circumstances. Passing $this to a constructor or a method is totally fine, since you're passing a handle to the object rather than duplicating the object. The thing to be careful of is creating circular references, which in PHP 5.2 equate to memory leaks. In this case, simply pulling data off of the creating object and not saving the object itself in the second class is fine and creates no circular reference (unless one of those properties is also an object), so all is right with the world. --Larry Garfield On Monday, November 29, 2010 9:21:37 pm Junkmail wrote: > I am having a hard time understanding why one would need/want to use this > technique. Can you explain a bit about the theory behind this pattern? I > don't think I even understand how PHP would handle that. Creating a class > and then setting it to another class? > > "David Abdemoulaie" <da...@ho...> wrote: > >Pass $this to the "Other_Class" via constructor: > > $this->other_class = new Other_Class($this); > > > >Both x and y will need to have public visibility in order for > >Other_Class to access them directly like that. Otherwise you will need > >to implement __get() or use specific getters. > > > >On Nov 29, 2010, at 6:20 PM, Walter wrote: > >> I have 2 classes, the first instantiates the second... > >> > >> $this->other_class = new Other_Class; > >> > >> Now I would like to the constrictor of Other_Class "know" who called > >> it and pull property data from that class. > >> > >> Say I have this.. > >> > >> $this->x = 6; > >> $this->y = true; > >> > >> How can the constructor of Other_Class pull the values of 'x' and 'y' > >> from the first class? > >> > >> Any ideas? > >> > >> Thanks > >> > >> Walter |
From: Walter <php...@to...> - 2010-11-30 17:09:00
|
Thank you gentlemen. You gave me the answer I was afraid of. I was hoping for a more classical OO solution, but, we work with what we have. Thanks again for the education. Walter |
From: <la...@ga...> - 2010-11-30 17:32:38
|
That is the classical OO solution. Passing objects to objects is classically OO, whether you're in a "classic" (ie, class-using) OO language or not. Why is that a frightening answer? --Larry Garfield On 11/30/10 11:08 AM, Walter wrote: > Thank you gentlemen. > > You gave me the answer I was afraid of. I was hoping for a more > classical OO solution, but, we work with what we have. > > Thanks again for the education. > > Walter |
From: CM L. <cmc...@gm...> - 2010-11-30 17:45:20
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Perhaps you are thinking of inner classes (where the outer scope is inherited in the inner scope). In this situation, you could have (pseudo code) class A { String x = "Outer Scope"; class B { public B { println(x); } } public static void run { B b = new B(); } } A::run(); PHP does not allow nesting classes, though you can use namespaces (5.3) to achieve similar results. Hope that helps, CM Lubinski - --- http://cmlubinski.info On 11/30/2010 11:08 AM, Walter wrote: > Thank you gentlemen. > > You gave me the answer I was afraid of. I was hoping for a more > classical OO solution, but, we work with what we have. > > Thanks again for the education. > > Walter > > ------------------------------------------------------------------------------ > Increase Visibility of Your 3D Game App & Earn a Chance To Win $500! > Tap into the largest installed PC base & get more eyes on your game by > optimizing for Intel(R) Graphics Technology. Get started today with the > Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs. > http://p.sf.net/sfu/intelisp-dev2dev > _______________________________________________ > chiPHPug-discuss mailing list > chi...@li... > https://lists.sourceforge.net/lists/listinfo/chiphpug-discuss -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iEYEARECAAYFAkz1OBAACgkQfzi1OiZiJLAS3gCePzgJAz4OrNDYg/zqafLCuVQB N2wAnRqPAFx0X1EYGG7L4/m1LTGM4bN3 =3yE8 -----END PGP SIGNATURE----- |
From: Arlo L. <ar...@ar...> - 2010-11-30 22:22:58
|
Well if that's resolved, then here's a similar question I've wondered about. What's the best way to access constants and static methods of a child class from a parent class if there's more than one possible child class? I mean, I could call Child::CONSTANT or Child::method() if I know the child's name is Child, but I don't. The best I've come up with is a combination of eval and get_class(): <?php class Child extends Base { protected $size = 10; const USE_SESSIONS = false; public static function childMethod($step) { return "calling child method from step $step<br />"; } } class Base { function __construct() { // getting a property is no problem print "my size is ".$this->size."<br />"; // is there a cleaner way to get a constant? eval("\$use_sessions = ".get_class($this)."::USE_SESSIONS;"); print ($use_sessions) ? "using sessions<br />" : "not using sessions<br />" ; // test // is there a cleaner way to call a static method? eval("print ".get_class($this)."::childMethod(1);"); // this seems to work, actually ... is this valid? // I thought you had to use :: to call a static method print $this->childMethod(2); // this is better but requires PHP 5.3+ $classname = get_class($this); $use_sessions = $classname::USE_SESSIONS; print $classname::childMethod(3); } } $child = new Child(); ?> _______________________________ Arlo Leach 773.769.6106 http://arlomedia.com Honor two blues pioneers: http://mccoybrotherstribute.com |
From: CM L. <cmc...@gm...> - 2010-11-30 22:41:21
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I believe you need late static binding for this (which was added in PHP 5.3). If you are running 5.3, you can use the "static" key word. e.g. class A { const MYNAME = "A"; function __construct() { echo static::MYNAME . "\n"; } } class B extends A { const MYNAME = "B"; } $a = new A(); // "A" $b = new B(); // "B" Prior to 5.3, you'd need to copy and "override" the same function to all children to achieve a similar style of late binding. e.g. class A { const MYNAME = "A"; function __construct() { echo self::MYNAME . " " . $this->myName() . "\n"; } function myName() { return self::MYNAME; } } class B extends A { const MYNAME = "B"; function myName() { return self::MYNAME; } } $a = new A(); // "A A" $b = new B(); // "A B" Moral of the story: Use 5.3 On 11/30/2010 04:09 PM, Arlo Leach wrote: > Well if that's resolved, then here's a similar question I've wondered about. What's the best way to access constants and static methods of a child class from a parent class if there's more than one possible child class? I mean, I could call Child::CONSTANT or Child::method() if I know the child's name is Child, but I don't. The best I've come up with is a combination of eval and get_class(): > > <?php > > class Child extends Base { > > protected $size = 10; > > const USE_SESSIONS = false; > > public static function childMethod($step) { > return "calling child method from step $step<br />"; > } > > } > > class Base { > > function __construct() { > // getting a property is no problem > print "my size is ".$this->size."<br />"; > > // is there a cleaner way to get a constant? > eval("\$use_sessions = ".get_class($this)."::USE_SESSIONS;"); > print ($use_sessions) ? "using sessions<br />" : "not using sessions<br />" ; // test > > // is there a cleaner way to call a static method? > eval("print ".get_class($this)."::childMethod(1);"); > > // this seems to work, actually ... is this valid? > // I thought you had to use :: to call a static method > print $this->childMethod(2); > > // this is better but requires PHP 5.3+ > $classname = get_class($this); > $use_sessions = $classname::USE_SESSIONS; > print $classname::childMethod(3); > } > > } > > $child = new Child(); > > ?> > > _______________________________ > > Arlo Leach > 773.769.6106 > http://arlomedia.com > > Honor two blues pioneers: > http://mccoybrotherstribute.com > > > ------------------------------------------------------------------------------ > Increase Visibility of Your 3D Game App & Earn a Chance To Win $500! > Tap into the largest installed PC base & get more eyes on your game by > optimizing for Intel(R) Graphics Technology. Get started today with the > Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs. > http://p.sf.net/sfu/intelisp-dev2dev > _______________________________________________ > chiPHPug-discuss mailing list > chi...@li... > https://lists.sourceforge.net/lists/listinfo/chiphpug-discuss -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iEUEARECAAYFAkz1fYQACgkQfzi1OiZiJLB63wCaAvSUmPgVVsgs4Q1xp5EHjnLM w5UAli11RTNBqyHupyrJSbtSJgchgQc= =3iWV -----END PGP SIGNATURE----- |
From: Arlo L. <ar...@ar...> - 2010-12-01 17:19:42
|
> I believe you need late static binding for this (which was added in PHP > 5.3). If you are running 5.3, you can use the "static" key word. > > Prior to 5.3, you'd need to copy and "override" the same function to all > children to achieve a similar style of late binding. Got it. The late static binding is exactly what I needed for 5.3+. For pre-5.3, the override approach makes sense but only works for methods; it looks like a constant in a child class does not override a constant in the parent class. (But that can also be solved by adding a getter method to the child class, as you demonstrated.) Fortunately I have less need for pre-5.3 compatibility than I did a year ago when I first ran across this. Thanks, -Arlo _______________________________ Arlo Leach 773.769.6106 http://arlomedia.com Honor two blues pioneers: http://mccoybrotherstribute.com |
From: Neil R. <Nei...@rc...> - 2010-11-30 03:31:43
|
Is debug_backtrace() any help? Wherever you are, you can find out how you got there . . . At 06:20 PM 11/29/2010, you wrote: >I have 2 classes, the first instantiates the second... > > $this->other_class = new Other_Class; > >Now I would like to the constrictor of Other_Class "know" who called >it and pull property data from that class. > >Say I have this.. > > $this->x = 6; > $this->y = true; > >How can the constructor of Other_Class pull the values of 'x' and 'y' >from the first class? > >Any ideas? Neil Rest -- Nei...@rc... Anyone who has never made a mistake has never tried anything new. -- Albert Einstein |