late-static-binding

late static binding in PHP

…衆ロ難τιáo~ 提交于 2019-12-03 16:34:12
I am reading the php manual about the LSB feature, I understand how it works in the static context, but I don't quite understand it in the non-static context. The example in the manual is this: <?php class A { private function foo() { echo "success!\n"; } public function test() { $this->foo(); static::foo(); } } class B extends A { /* foo() will be copied to B, hence its scope will still be A and * the call be successful */ } class C extends A { private function foo() { /* original method is replaced; the scope of the new one is C */ } } $b = new B(); $b->test(); $c = new C(); $c->test(); /

Why return new static? (PHP)

耗尽温柔 提交于 2019-12-03 04:53:21
问题 Why some people create one method that returns new static instead of making all of the methods static? What is the reason to have that method that returns new static ? I am not asking what is the difference between static and self, or what static & self mean. For example, here is one simple class: <?php class Expression { public static function make() { return new static; } public function find($value) { return '/' . $value .'/'; } public function then($value) { return $this->find($value); }

Is there a way to have PHP subclasses inherit properties (both static and instance)?

时间秒杀一切 提交于 2019-12-01 18:55:11
If I declare a base class as follows: abstract class Parent { protected static $message = "UNTOUCHED"; public static function yeah() { static::$message = "YEAH"; } public static function nope() { static::$message = "NOPE"; } public static function lateStaticDebug() { return(static::$message); } } and then extend it: class Child extends Parent { } and then do this: Parent::yeah(); Parent::lateStaticDebug(); // "YEAH" Child::nope(); Child::lateStaticDebug(); // "NOPE" Parent::yeah(); Child::lateStaticDebug() // "YEAH" Is there a way to have my second class that inherits from the first also

Inherit static properties in subclass without redeclaration?

点点圈 提交于 2019-11-29 13:33:56
I'm having the same problem as this guy with the application I'm writing right now. The problem is that static properties are not being inherited in subclasses, and so if I use the static:: keyword in my main class, it sets the variable in my main class as well. It works if I redeclare the static variables in my subclass, but I expect to have a large number of static properties and subclasses and wish to avoid code duplication. The top-rated response on the page I linked has a link to a few "workarounds", but it seems to have 404'd. Can anyone lend me some help or perhaps point me in the

PHP 5.3: Late static binding doesn't work for properties when defined in parent class while missing in child class

心已入冬 提交于 2019-11-27 16:21:54
问题 Take a look at this example, and notice the outputs indicated. <?php class Mommy { protected static $_data = "Mommy Data"; public static function init( $data ) { static::$_data = $data; } public static function showData() { echo static::$_data . "<br>"; } } class Brother extends Mommy { } class Sister extends Mommy { } Brother::init( "Brother Data" ); Sister::init( "Sister Data" ); Brother::showData(); // Outputs: Sister Data Sister::showData(); // Outputs: Sister Data ?> My understanding was

What exactly are late static bindings in PHP?

与世无争的帅哥 提交于 2019-11-26 02:05:49
问题 What exactly are late static bindings in PHP? 回答1: You definitely need to read Late Static Bindings in the PHP manual. However, I'll try to give you a quick summary. Basically, it boils down to the fact that the self keyword does not follow the same rules of inheritance. self always resolves to the class in which it is used. This means that if you make a method in a parent class and call it from a child class, self will not reference the child as you might expect. Late static binding