Page 1 of 1
Static call - get class
Posted: Fri Mar 14, 2008 8:38 am
by jonaphin
Hey Guys,
I'm trying to call a static method as such:
User::find(123);
Where User extends MyObject and find is a static method defined in MyObject;
Calling get_class() from 'find' in MyObject returns 'MyObject' even though User::find() was called.
Anyone has any idea on how to get around that caveat and have the find static method in MyObject know that 'User' called it ?
I really don't want to have to create a static find method in children objects of MyObject.
Any idea on the subject is deeply appreciated.
Jonathan
Re: Static call - get class
Posted: Fri Mar 14, 2008 3:33 pm
by Christopher
You can't do what you want currently in PHP (maybe 5.3 or 6?). Trust me, even Zend gave up on a Ruby style ActiveRecord implementation in ZF. Implement the static finder as a function or create an object.
Re: Static call - get class
Posted: Fri Mar 14, 2008 3:54 pm
by jonaphin
Bleh... I'd hate to have to get rid of that nice factory method... it seemed so elegant :p
I guess I may have to go with instantiating the user class and making the find method a regular one... then switch to Rails exclusively...

Re: Static call - get class
Posted: Fri Mar 14, 2008 4:11 pm
by Christopher
jonaphin wrote:Bleh... I'd hate to have to get rid of that nice factory method... it seemed so elegant :p
I am not sure "elegant" is the word ... perhaps "easy short-cut". Not sure what you are doing is a Factory either. A function will do what you want, which is using a static to create initialized objects.
Re: Static call - get class
Posted: Fri Mar 14, 2008 4:47 pm
by jonaphin
I was going to mention using a function as a possible solution... then erased that.
$user = find('User', 123);
That breaks the OO fun. I might just have to resign myself to using this if I can't get my Rails way with PHP (Please don't laugh!). And I do find elegance in that pattern.
Les gouts et les couleurs ne se discutent pas... To each his own!
Thank you for your precious help

Re: Static call - get class
Posted: Fri Mar 14, 2008 5:13 pm
by Christopher
jonaphin wrote:I was going to mention using a function as a possible solution... then erased that.
$user = find('User', 123);
$user = User_find(123); with static locals is the same as what you are trying to do.
jonaphin wrote:That breaks the OO fun. I might just have to resign myself to using this if I can't get my Rails way with PHP (Please don't laugh!). And I do find elegance in that pattern.
$user = User::find(123); has already done that -- it's not OO.
Re: Static call - get class
Posted: Fri Mar 14, 2008 5:21 pm
by anto91
What part of that isnt Object Oriented? you can use Completly static classes an example is the Zend_Registry class.
Re: Static call - get class
Posted: Fri Mar 14, 2008 5:35 pm
by Christopher
So you are using another non-OO example as your evidence?

Re: Static call - get class
Posted: Fri Mar 14, 2008 11:24 pm
by yacahuma
I think you want a factory pattern
take a look at
http://www.ibm.com/developerworks/libr ... ignptrns/
see if that is what you want. Check the factory2.php
Re: Static call - get class
Posted: Sat Mar 15, 2008 10:59 am
by Chris Corbyn
Code: Select all
$user = MyObject::find(new User(), 42);
Re: Static call - get class
Posted: Mon Mar 17, 2008 8:45 am
by jonaphin
Seriously, thanks for the help, but none of these solutions fit my taste.
I finally hacked freakin PHP at 2 AM on Saturday night and had it understand User::find(123) the way it should, using the reflection API.
Thank you all!