Magically Finding a 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
zoborg
Forum Newbie
Posts: 1
Joined: Mon Oct 04, 2010 11:30 am

Magically Finding a class

Post by zoborg »

Hello folks,

Sorry if my questions are a little lame, I am a Perl dev at heart and am doing some projects in Zend/PHP!, this is a much simpler problem than I want to solve, but basically I want to be able to dynamically resolve class names and call as static method on that class.


lets say

Code: Select all

class Person extends MyBaseClass {
       public static  $_test = 'Person';
       public static function tell() {
            echo Person::$_test;
      }
}
class Item  extends MyBaseClass {
       public static $_test = "Item";
      
       public static function tell() {
            echo Item::$_test;
      }
}
So I could of course do Item::tell(); or Person::tell();

However I want to factor out the 'tell' function to the MyBaseClass as its common to both, however how would I automagically call the correct static class name? if that makes sense?

I tried a experiment like '

Code: Select all

$class = 'Person';
$class::tell();
But that gives a error?

Any ideas?
$
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Magically Finding a class

Post by John Cartwright »

You can get the class name from the parent function using

Code: Select all

get_class($this);
EDIT | Just realized you are using static methods, therefore, you need to use PHP >= 5.3 feature called Late Static Bindings

Code: Select all

class Parent 
{
   static public function myAction()
   {
      echo get_called_class();
   }
}

class Child extends Parent
{
}

Child::myAction(); //echo's Child
EDIT2 | If you are not using the latest version of PHP, you could probably hack up a debug_backtrace() workaround
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Magically Finding a class

Post by Jonah Bron »

Couldn't you just call self::_test ?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Magically Finding a class

Post by John Cartwright »

Jonah Bron wrote:Couldn't you just call self::_test ?
If you have 5.3, there is no reason to implement the a hardcoded variable of the class name. However, your right though, the the current implementation it would make more self the use the self keyword.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Magically Finding a class

Post by AbraCadaver »

You can use self if you are calling from in the class, but if outside:

Code: Select all

$class = 'Person';
call_user_func(array($class, 'tell'));
//or
call_user_func("$class::tell");
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Magically Finding a class

Post by Eran »

It's probably better if you avoid this kind of dynamic static classes. Use objects and regular inheritance instead
Post Reply