1

I have two classes:

class JController{
   public static function getInstance()
   {
       //some source, not important...
       self::createFile();// 
   }

   public static function createFile()
   {
       // this is base class method
   }
}

class CustomController extends JController{

   public static function createFile()
   {
       // this is overriden class method
   }
}

And I am trying to call static method on derived class which calls parents method and not overriden. Is it expected behaviour?

That's how I try to use it:

$controllerInstance = CustomController::getInstance();

My question is: why doesn't CustomController::getInstance() call on CustomController::createFile()?

insanebits
  • 818
  • 1
  • 6
  • 24

2 Answers2

6

That is expected behavior. Before php 5.3 static methods will only call the method from the first definition in the hierarchy. 5.3+ has late static binding support and with that the ability to use the method directly on the child class. To do this you need to use the static keyword instead of self:

   public static function getInstance()
   {
       //some source, not important...
       static::createFile();// 
   }
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • Thanks for the answer, but maybe there other way around it? Because class I am overriding is Joomla's, by the way your answer was first and I will accept in couple of minutes – insanebits Aug 01 '13 at 14:35
  • Not that i can think of without modifying a core class. That said depending upon the logic of createFile, and exactly what you want the end result to be there might be another way. – prodigitalson Aug 01 '13 at 14:45
  • I only needed to call my static method instead of one from the base class, so I have copied source of that method and changed `self` to `static` and it worked like a charm :) – insanebits Aug 01 '13 at 14:54
3

Late Static Binding:

use

static::createFile();

instead of

self::createFile();
Mark Baker
  • 209,507
  • 32
  • 346
  • 385