A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearCan PHP do a fairly trivial inheritance thing? - page 1
User InfoPosts
Can PHP do a fairly trivial inheritance thing?#1
I have the following code:

abstract class AbstractParent {
function __construct($param) { print_r($param); }
public static function test() { return new self(1234); }
}

class SpecificClass extends AbstractParent {}


When I invoke SpecificClass::test(), I am getting an error:

Fatal error: Cannot instantiate abstract class AbstractParent


So what I basically want is just to let AbstractParent's test() instantiate class where this test() was called from (so, in my example, instantiate SpecificClass).

posted date: 2008-12-14 18:25:00


Re: Can PHP do a fairly trivial inheritance thing?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-14 18:25:01


Re: Can PHP do a fairly trivial inheritance thing?#3
You can do it in PHP 5.3, which is still in alpha. What you're looking for is called Late-Static-Binding. You want the parent class to refer to the child class in a static method. You can't do it yet, but it's coming...Edit: You can find more info here - http://www.php.net/manual/en/language.oop5.late-static-bindings.php

posted date: 2008-12-14 18:29:00


Re: Can PHP do a fairly trivial inheritance thing?#4
Prior version 5.3 Only with the following work around:abstract class AbstractParent { function __construct($param) { print_r($param); } abstract public static function test(); private static function test2($classname) { return new $classname(1234); }}class SpecificClass extends AbstractParent { public static function test() {return self::test2(__CLASS__);}}

posted date: 2008-12-14 18:37:00


Re: Can PHP do a fairly trivial inheritance thing?#5
I've been seeing people complain a lot about late static binding lately and I'm curious: What other languages commonly used for web development do support late static binding? Python? Ruby? Perl? Java?

posted date: 2008-12-14 18:39:00


Re: Can PHP do a fairly trivial inheritance thing?#6
all of them, afair. I used Ruby extensively over past couple of years.

posted date: 2008-12-14 18:46:00


Re: Can PHP do a fairly trivial inheritance thing?#7
You don't have to have the two static methods named differently. You can call them "test" in both classes and have SpecificClass::test return parent::test.

posted date: 2009-10-21 16:46:00


Re: Can PHP do a fairly trivial inheritance thing?#8
possible duplicate of Inheritance of static members in PHP

posted date: 2012-07-01 07:43:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.