instantiate derived class objects from within base class?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



All,

I get a fatal error when trying to create a new instance of a derived class from within a static member which exists in the parent base class. I think the problem is related to using this call:

   new self();

Because the 'self' refers to the parent object and not the derived class. See the code below and the error message which follows:

---------- 8< -------------------- 8< -------------------- 8< ----------
<?php
// defines base functions and requires derived class to replace foo()
class A {
   protected abstract function foo();
   public static function bar() {
       $x = new self();
       $x->foo();
   }
}
// defines foo() as it should, but base class doesn't know foo exists
// when it references self()
class B extends A {
   protected function foo() {
       echo "hello world";
   }
}

// show how broken things are
B :: bar();
?>
---------- 8< -------------------- 8< -------------------- 8< ----------

PHP Fatal error: Class A contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (A::foo) in .../demo.php on line 9

How do I refer to the derived class from within the base class?

Dante

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux