Tag: static


Snippet: PHP Calling static class methods

December 1st, 2009 — 11:57am

Given the example class foo:

class foo {
  public static function myFunc() {
    return "foo - myFunc";
  }
}

The class name is known in the calling context:

  echo foo::myFunc();

The class name is stored in a php var:

  $class_name = "foo";
 
  /** PHP 5.2.x */
  echo call_user_func(array($class_name, "myFunc"));  
 
  /** PHP 5.3.x */
  echo $class_name::myFunc();

More info on call_user_func: http://es.php.net/manual/en/function.call-user-func.php
More info on static keyword: http://es.php.net/manual/en/language.oop5.static.php

Comment » | php

Back to top