Static call - get class
Moderator: General Moderators
Static call - get class
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
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
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Static call - get class
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.
(#10850)
Re: Static call - get class
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...
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...
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Static call - get class
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.jonaphin wrote:Bleh... I'd hate to have to get rid of that nice factory method... it seemed so elegant :p
(#10850)
Re: Static call - get class
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
$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
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Static call - get class
$user = User_find(123); with static locals is the same as what you are trying to do.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); has already done that -- it's not OO.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.
(#10850)
Re: Static call - get class
What part of that isnt Object Oriented? you can use Completly static classes an example is the Zend_Registry class.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Static call - get class
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
take a look at http://www.ibm.com/developerworks/libr ... ignptrns/
see if that is what you want. Check the factory2.php
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Static call - get class
Code: Select all
$user = MyObject::find(new User(), 42);Re: Static call - get class
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!
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!