开发者

PHP Abstract Method Resolution

开发者 https://www.devze.com 2022-12-14 13:10 出处:网络
I have an abstract class defined as follows: abstract class Abstract Parent extends Zend_Db_Table_Abstract {

I have an abstract class defined as follows:

abstract class Abstract Parent extends Zend_Db_Table_Abstract { 
    abstract function funcA($post);
    abstract function funcB();

    public function newEntry($post) {  
        $t1 = $this->funcA($post);
        $t2 = $this->funcB();
    }
}

The child class defines the two abstract methods, as follows:

require_once 'atablemodel.php';

class Child extends AbstractParent {    
    public function funcB() 
    {
        return 'Some Value';
    }

    public function funcA($post) 
    {
        $data = array(
            'v1'    => htmlentities($post['v1'])
        );
        return $data;
    }
}

However, when I try this, I get an error:

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /var/www/.../abstractparent.php on line 27

which is the line where the abstract parent is calling one of the abstract methods. What I want to have happen is that this line should call the child method, which is defined.

Now, I assume that there is a way to do this, and since I'm a beginner to PHP, I'm doing so开发者_高级运维mething fundamentally wrong. Any suggestions as to what I might do to resolve this? If I were to define the two abstract methods as having implementations, and then overriding those methods in all children (that is, not deal with abstract classes at all), how would I ensure that the parent calling one of those methods would call the appropriate child method at execution time?

EDIT

In light of the various comments about the issue of combining the static and abstract, I redefined the classes as above, with a new error, also shown above.


A static method cannot use $this to reference itself, as a static function can be utilized without instantiating the class, and therefore $this can't be guaranteed to exist.

The self keyword can be used to reference methods in the class tree. You'd want to implement it like this:

public static function newEntry($post)      {  
    $t1 = self::funcA($post);
    $t2 = self::funcB();
}

However, be aware that there are issues surrounding Late Static Binding of class structures. PHP 5.3 can work differently than previous versions for this kind of scope resolution, so be sure to read up on it. If the parent/child structure doesn't work properly for you, you can always reference a static method by exact classname as well, ie. Child::funcA.


To address a method of the current object, you need $this:

    $t1 = $this->funcA($post);
    $t2 = $this->funcB();

calling funcA() alone looks for a globally defined function, not a method.

If you want to call the method from outside the object, use

$objectname->methodname()


You declared the functions as static, so you can't use them with $this. Static methods can be referenced with self:: or via the classname::

You use $this on an instance of the class on non-static methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消