0

I have the following:

class HP_Utils {
/**********************************************************************/
public static function getAction()
/**********************************************************************/
{
  if( isset( $_GET[ 'action' ] ) ) {
    return $_GET[ 'action' ];
  }
  else if ( isset( $_POST[ 'action' ] )) {
    return $_POST[ 'action' ];
  }
}
/**********************************************************************/
}

Then in other class:

$this->utils = $this->load( 'helper', 'Utils' );
$action = $this->utils::getAction(); <============== ERROR

The loader:

abstract class MController {
/**********************************************************************/
protected function load( $type, $className ) {
  switch( $type ) {
    case 'model':
      $name = 'MD_' . $className;
      break;
    case 'view':
      $name = 'VW_' . $className;
      break;
    case 'helper';
      $name = 'HP_' . $className;
      break;
  }
  $path = $type . '/' . $name . '.php';
  include( $path );
  return new $name;
}

Gives me error:

PHP Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) in /home/jorgee/www/menu/controller/CT_Menu.php on line 11

Obviously i'm badly accesing the static method but I thought that was the way, any help?

6
  • Why are trying to store a static class into a variable? Static methods can be access globally without an instance with ClassName::methodName().
    – Shoe
    Commented Jan 7, 2013 at 17:30
  • Your loader instantiates the util class object, why not just access it as a regular method call $this->util->getAction(); (which is perfectly ok even though it's a static, it can also be called as an instance method)
    – Crisp
    Commented Jan 7, 2013 at 17:33
  • @Crisp it's not perfectly valid at all. It's contrary to all OOP principles, and only possible because PHP allows it, as its OOP functionnalities were once weak, and now they keep the BC Commented Jan 7, 2013 at 19:10
  • @ClementHerreman understood, I meant perfectly ok simply from a PHP interpreter standpoint.
    – Crisp
    Commented Jan 7, 2013 at 19:18
  • @ClementHerreman Sorry to harp back to this Clement, but you mention deprecation errors in your other comment, but there are none, at least as of php5.4.9, and indeed the man page for static keyword says A property declared as static can not be accessed with an instantiated class object (**though a static method can**). No mention there of an impending deprecation of that functionality.
    – Crisp
    Commented Jan 7, 2013 at 19:33

2 Answers 2

1

$this->load() return an object, not a class. Thus when calling $this->utils::getAction(), you get an error.

I'd say use the class name directly, HP_Utils::getAction(), but if you need it to be dynamic, you can use call_user_func :

abstract class MController {
/**********************************************************************/
protected function load( $type, $className ) {
  switch( $type ) {
    case 'model':
      $name = 'MD_' . $className;
      break;
    case 'view':
      $name = 'VW_' . $className;
      break;
    case 'helper';
      $name = 'HP_' . $className;
      break;
  }
  $path = $type . '/' . $name . '.php';
  include( $path );
  return $name;
}

//The actual calling code :
$this->utils = $this->load( 'helper', 'Utils' );
$action = call_user_func($this->utils . '::getAction()');

Please note that this is a very ugly way to call some method. You should question yourself whether you have a design problem here.

1

The right way would be to replace this:

$action = $this->utils::getAction(); <============== ERROR

with this:

$action = HP_Utils::getAction();

I believe you can also call a static method in PHP as if it was an instance method, like so:

$action = $this->utils->getAction();

but the first version is generally better as it makes it very clear that you are in fact calling a static method and not an instance method.

3
  • Is there a reason why I can't call the static method from the instance? (Maybe OOP basic theory :P)
    – JorgeeFG
    Commented Jan 7, 2013 at 17:31
  • @Jorge - just edited my answer - I believe you can actually do that in PHP, or at least you could in some older versions. Commented Jan 7, 2013 at 17:33
  • @Jorge indeed, basic OOP theory. Static method, are - as their name tell - class-wide, not object-wide, so you call them from the class. PHP tolerate this because of some old BC from PHP 4.x, but you should use it. I believe this will raise some deprecation errors Commented Jan 7, 2013 at 17:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.