Re: [PHP] context when calling non static method of class in a static way
ah i forgot e_all doesnt include e_strict. with error_reporting(-1 / E_ALL | E_STRICT) i see the errors. so i think i am right that the use of that special behavior of php is not a good idea. thank you guys! Am 23.05.2011 00:32, schrieb Richard Quadling: On 22 May 2011 22:44, Simon Hilz wrote: i cant reproduce that error. which php version do you use? i've coded an example for a "behavior"-pattern: Try with ... call TankUpBehavior::tankUp (100) Strict Standards: Non-static method TankUpBehavior::tankUp() should not be called statically, assuming $this from incompatible context in D:\Work\t1.php on line 50 Fuel after tank up 100 l: 100call DriveBehavior::drive (24) Strict Standards: Non-static method DriveBehavior::drive() should not be called statically, assuming $this from incompatible context in D:\Work\t1.php on line 50 Fuel after driving 24 km: 98.272 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] context when calling non static method of class in a static way
On 22 May 2011 22:44, Simon Hilz wrote: > i cant reproduce that error. which php version do you use? > i've coded an example for a "behavior"-pattern: > Try with ... call TankUpBehavior::tankUp (100) Strict Standards: Non-static method TankUpBehavior::tankUp() should not be called statically, assuming $this from incompatible context in D:\Work\t1.php on line 50 Fuel after tank up 100 l: 100call DriveBehavior::drive (24) Strict Standards: Non-static method DriveBehavior::drive() should not be called statically, assuming $this from incompatible context in D:\Work\t1.php on line 50 Fuel after driving 24 km: 98.272 -- Richard Quadling Twitter : EE : Zend : PHPDoc @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] context when calling non static method of class in a static way
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).")"; $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().""; $bmw->tankUp(100); echo "Fuel after tank up 100 l: ".$bmw->getFuel().""; $bmw->drive(24); echo "Fuel after driving 24 km: ".$bmw->getFuel().""; === 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] context when calling non static method of class in a static way
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 -- WWW: plphp.dk / plind.dk LinkedIn: plind BeWelcome/Couchsurfing: Fake51 Twitter: kafe15 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] context when calling non static method of class in a static way
the good thing about this "bug" would be, that it's not neccesary to extend a class to use its methods as if they would be defined in the extending class. this way i think it would be possible to implement something like multiple inheritance, which is currently not (that easy) possible. when i'll find time i'll test how attributes behave that way. Am 22.05.2011 22:31, schrieb ad...@buskirkgraphics.com: Correct that. I do not know what I was even thinking when I wrote that response. It does not make any sense what so ever. The class is already extended of course I can call a method from it. Sorry Richard L. Buskirk -Original Message- From: Simon Hilz [mailto:simon.h...@gmx.de] Sent: Sunday, May 22, 2011 11:56 AM To: php-general@lists.php.net Subject: Re: [PHP] context when calling non static method of class in a static way Richard, yes! at least my example works. i didn't test it any further; i doubt it is intended that way. Simon Hilz Am 22.05.2011 16:42, schrieb ad...@buskirkgraphics.com: Simon, So without extending foo you can run bar in another class? Richard L. Buskirk -Original Message- From: Simon Hilz [mailto:simon.h...@gmx.de] Sent: Sunday, May 22, 2011 10:18 AM To: php-general@lists.php.net Subject: [PHP] context when calling non static method of class in a static way hi, lets assume the following classes: class Foo{ public function bar() { echo get_class($this); } } class Foobar{ public function callBarStatic() { Foo::bar(); } } the following code results in the output "Foobar": $obj = new Foobar(); $obj->callBarStatic(); That means that the static call of bar() is executed in the context of Foobar. Is this behavior deliberate? If so, it would open a great way of object composition patterns. But only if it will be retained in future versions :) (i've tested with 5.3.5) Simon Hilz -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] context when calling non static method of class in a static way
Correct that. I do not know what I was even thinking when I wrote that response. It does not make any sense what so ever. The class is already extended of course I can call a method from it. Sorry Richard L. Buskirk -Original Message- From: Simon Hilz [mailto:simon.h...@gmx.de] Sent: Sunday, May 22, 2011 11:56 AM To: php-general@lists.php.net Subject: Re: [PHP] context when calling non static method of class in a static way Richard, yes! at least my example works. i didn't test it any further; i doubt it is intended that way. Simon Hilz Am 22.05.2011 16:42, schrieb ad...@buskirkgraphics.com: > Simon, > So without extending foo you can run bar in another class? > > > Richard L. Buskirk > > > -Original Message- > From: Simon Hilz [mailto:simon.h...@gmx.de] > Sent: Sunday, May 22, 2011 10:18 AM > To: php-general@lists.php.net > Subject: [PHP] context when calling non static method of class in a static > way > > hi, > > lets assume the following classes: > > class Foo{ > > public function bar() > { > echo get_class($this); > } > > } > > class Foobar{ > > public function callBarStatic() > { > Foo::bar(); > } > > } > > the following code results in the output "Foobar": > > $obj = new Foobar(); > $obj->callBarStatic(); > > That means that the static call of bar() is executed in the context of > Foobar. Is this behavior deliberate? If so, it would open a great way of > object composition patterns. But only if it will be retained in future > versions :) (i've tested with 5.3.5) > > > Simon Hilz > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] context when calling non static method of class in a static way
Simon, To be honest if it works, I hope they do not fix it. My only problem is that my classes are typically not in the same file but they are extended. I am going to try that on an extended class and see if I can instantiate a method from another class in a separate file. Richard L. Buskirk -Original Message- From: Simon Hilz [mailto:simon.h...@gmx.de] Sent: Sunday, May 22, 2011 11:56 AM To: php-general@lists.php.net Subject: Re: [PHP] context when calling non static method of class in a static way Richard, yes! at least my example works. i didn't test it any further; i doubt it is intended that way. Simon Hilz Am 22.05.2011 16:42, schrieb ad...@buskirkgraphics.com: > Simon, > So without extending foo you can run bar in another class? > > > Richard L. Buskirk > > > -Original Message- > From: Simon Hilz [mailto:simon.h...@gmx.de] > Sent: Sunday, May 22, 2011 10:18 AM > To: php-general@lists.php.net > Subject: [PHP] context when calling non static method of class in a static > way > > hi, > > lets assume the following classes: > > class Foo{ > > public function bar() > { > echo get_class($this); > } > > } > > class Foobar{ > > public function callBarStatic() > { > Foo::bar(); > } > > } > > the following code results in the output "Foobar": > > $obj = new Foobar(); > $obj->callBarStatic(); > > That means that the static call of bar() is executed in the context of > Foobar. Is this behavior deliberate? If so, it would open a great way of > object composition patterns. But only if it will be retained in future > versions :) (i've tested with 5.3.5) > > > Simon Hilz > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] context when calling non static method of class in a static way
Mike, yes i know the difference. I actually discovered that by accident when i've forgot to write the static keyword. my code lead to an exception. i wondered about the details of that exception and came to the solution that the behavior as decribed exists. in my opinion one could really use that behavior for a design pattern in order to dynamically add abilities to objects. (e.g. implement __call interceptor and statically call the method of another "ability provider"-class statically. it would behave just like a native function of that object.) Am 22.05.2011 16:47, schrieb Mike Mackintosh: Simon, You may want to be careful with the way you declare your class methods. Example: public function bar() != static function bar(), even if you use pnysudsfksdljfasdjfsd (::) See the example below. class Foo{ static function barStatic() { echo get_class($this); } public function barPublic() { echo get_class($this); } } class Foobar{ public function callBarStatic() { Foo::barStatic(); } public function callBarPublic() { Foo::barPublic(); } } $oo = new Foobar; $oo->callBarStatic(); // returns only Foo $oo->callBarPublic(); // returns Foobar On May 22, 2011, at 10:17 AM, Simon Hilz wrote: hi, lets assume the following classes: class Foo{ public function bar() { echo get_class($this); } } class Foobar{ public function callBarStatic() { Foo::bar(); } } the following code results in the output "Foobar": $obj = new Foobar(); $obj->callBarStatic(); That means that the static call of bar() is executed in the context of Foobar. Is this behavior deliberate? If so, it would open a great way of object composition patterns. But only if it will be retained in future versions :) (i've tested with 5.3.5) Simon Hilz -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] context when calling non static method of class in a static way
Richard, yes! at least my example works. i didn't test it any further; i doubt it is intended that way. Simon Hilz Am 22.05.2011 16:42, schrieb ad...@buskirkgraphics.com: Simon, So without extending foo you can run bar in another class? Richard L. Buskirk -Original Message- From: Simon Hilz [mailto:simon.h...@gmx.de] Sent: Sunday, May 22, 2011 10:18 AM To: php-general@lists.php.net Subject: [PHP] context when calling non static method of class in a static way hi, lets assume the following classes: class Foo{ public function bar() { echo get_class($this); } } class Foobar{ public function callBarStatic() { Foo::bar(); } } the following code results in the output "Foobar": $obj = new Foobar(); $obj->callBarStatic(); That means that the static call of bar() is executed in the context of Foobar. Is this behavior deliberate? If so, it would open a great way of object composition patterns. But only if it will be retained in future versions :) (i've tested with 5.3.5) Simon Hilz -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] context when calling non static method of class in a static way
Simon, So without extending foo you can run bar in another class? Richard L. Buskirk -Original Message- From: Simon Hilz [mailto:simon.h...@gmx.de] Sent: Sunday, May 22, 2011 10:18 AM To: php-general@lists.php.net Subject: [PHP] context when calling non static method of class in a static way hi, lets assume the following classes: class Foo{ public function bar() { echo get_class($this); } } class Foobar{ public function callBarStatic() { Foo::bar(); } } the following code results in the output "Foobar": $obj = new Foobar(); $obj->callBarStatic(); That means that the static call of bar() is executed in the context of Foobar. Is this behavior deliberate? If so, it would open a great way of object composition patterns. But only if it will be retained in future versions :) (i've tested with 5.3.5) Simon Hilz -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] context when calling non static method of class in a static way
Simon, You may want to be careful with the way you declare your class methods. Example: public function bar() != static function bar(), even if you use pnysudsfksdljfasdjfsd (::) See the example below. class Foo{ static function barStatic() { echo get_class($this); } public function barPublic() { echo get_class($this); } } class Foobar{ public function callBarStatic() { Foo::barStatic(); } public function callBarPublic() { Foo::barPublic(); } } $oo = new Foobar; $oo->callBarStatic(); // returns only Foo $oo->callBarPublic(); // returns Foobar On May 22, 2011, at 10:17 AM, Simon Hilz wrote: > hi, > > lets assume the following classes: > > class Foo{ > > public function bar() > { > echo get_class($this); > } > > } > > class Foobar{ > > public function callBarStatic() > { > Foo::bar(); > } > > } > > the following code results in the output "Foobar": > > $obj = new Foobar(); > $obj->callBarStatic(); > > That means that the static call of bar() is executed in the context of > Foobar. Is this behavior deliberate? If so, it would open a great way of > object composition patterns. But only if it will be retained in future > versions :) (i've tested with 5.3.5) > > > Simon Hilz > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php