Re: context when calling non static method of class in a static way

From: Date: Sun, 22 May 2011 21:44:10 +0000
Subject: Re: context when calling non static method of class in a static way
References: 1 2 3 4 5 6  Groups: php.general 
Request: Send a blank email to php-general+get-313085@lists.php.net to get a copy of this message
i cant reproduce that error. which php version do you use? i've coded an example for a "behavior"-pattern: ========================================================= error_reporting(E_ALL & E_STRICT); class Car {
    private $fuel = 0;
    private $drivenDistance = 0;
    private $consumption = 0;
    private $behaviors = array();
    public function __construct($consumption)
        {
        $this->consumption = $consumption/100;
        }
    public function setFuel($fuel)
        {
        $this->fuel = $fuel;
        }
    public function getFuel()
        {
        return $this->fuel;
        }
    public function getConsumption()
        {
        return $this->consumption;
        }
    public function getDrivenDistance()
        {
        return $this->drivenDistance;
        }
    public function setDrivenDistance($drivenDistance)
        {
        $this->drivenDistance = $drivenDistance;
        }
    public function __call($name,$arguments)
        {
        foreach($this->behaviors as $behavior)
            {
            if(in_array($name,get_class_methods($behavior)))
                {
                echo "call $behavior::$name (".implode(",",$arguments).")<br>";
                $behavior::$name($arguments[0]);
                break;
                }
            }
        }
    public function addBehavior($name)
        {
        if(class_exists($name))
            {
            $this->behaviors[] = $name;
            }
        }
} class DriveBehavior
    {
    public function drive($distance)
        {
        $this->setDrivenDistance($this->getDrivenDistance()+$distance);
        $this->setFuel($this->getFuel()-$distance*$this->getConsumption());
        }
    }
class TankUpBehavior
    {
    public function tankUp($fuel)
        {
        $this->setFuel($this->getFuel()+$fuel);
        }
    }
$bmw = new Car(7.2); $bmw->addBehavior("TankUpBehavior"); $bmw->addBehavior("DriveBehavior"); echo "Fuel of my new BMW with consumption 7.2l/100km: ".$bmw->getFuel()."<br>"; $bmw->tankUp(100); echo "Fuel after tank up 100 l: ".$bmw->getFuel()."<br>"; $bmw->drive(24); echo "Fuel after driving 24 km: ".$bmw->getFuel()."<br>"; =================== OUTPUT: ============================== Fuel of my new BMW with consumption 7.2l/100km: 0 call TankUpBehavior::tankUp (100) Fuel after tank up 100 l: 100 call DriveBehavior::drive (24) Fuel after driving 24 km: 98.272 ========================================================= the strange thing: var_dump($this) always outputs the same object (as identified by id) but the "Behaviors" could only call the methods defined in Car if they are defined public. if they are protected or private they dont get called. no warning/error/whatever. just no call. attributes are not accessible too if defined protected or private but throws that error: Cannot access private property (more or less like expected) Simon Hilz Am 22.05.2011 23:18, schrieb Peter Lind:
class A {
     public function b() {
         echo get_class($this);
     }
     static function c() {
         echo get_class($this);
     }
} class B {
     public function test(){
         A::b();
         A::c();
     }
} $b = new B; $b->test(); Generates: Strict Standards: Non-static method A::b() should not be called statically, assuming $this from incompatible context in /tmp/test.php on line 14 B Notice: Undefined variable: this in /tmp/test.php on line 8 A I would never use code generating warnings and notices like that. I'd look into late static bindings instead: http://php.net/manual/en/language.oop5.late-static-bindings.php Regards Peter


« previous php.general (#313085) next »