Static call - get class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonaphin
Forum Newbie
Posts: 10
Joined: Sun May 07, 2006 2:47 pm

Static call - get class

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Static call - get class

Post 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.
(#10850)
jonaphin
Forum Newbie
Posts: 10
Joined: Sun May 07, 2006 2:47 pm

Re: Static call - get class

Post 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... :banghead:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Static call - get class

Post 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.
(#10850)
jonaphin
Forum Newbie
Posts: 10
Joined: Sun May 07, 2006 2:47 pm

Re: Static call - get class

Post 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 :drunk:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Static call - get class

Post 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.
(#10850)
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Static call - get class

Post by anto91 »

What part of that isnt Object Oriented? you can use Completly static classes an example is the Zend_Registry class.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Static call - get class

Post by Christopher »

So you are using another non-OO example as your evidence? :)
(#10850)
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Static call - get class

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Static call - get class

Post by Chris Corbyn »

Code: Select all

$user = MyObject::find(new User(), 42);
jonaphin
Forum Newbie
Posts: 10
Joined: Sun May 07, 2006 2:47 pm

Re: Static call - get class

Post 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!
Post Reply