late-static-binding

New self vs. new static

独自空忆成欢 提交于 2019-12-16 19:52:54
问题 I am converting a PHP 5.3 library to work on PHP 5.2. The main thing standing in my way is the use of late static binding like return new static($options); , if I convert this to return new self($options) will I get the same results? What is the difference between new self and new static ? 回答1: will I get the same results? Not really. I don't know of a workaround for PHP 5.2, though. What is the difference between new self and new static ? self refers to the same class in which the new

static:: vs. self:: - are there any downsides?

三世轮回 提交于 2019-12-07 13:18:48
问题 In this StackOverflow question I learned that self:: was not inheritance-aware where static:: was (in PHP). When it comes to defining a bunch of constants within a class, if you want to override those constants in a subclass to change default "behaviours", it becomes necessary to use static:: so that a method on the parent class that references the constant, honours the "override". In the 2 years since I asked that original question, I have started using static:: extensively, to the point

How to get child class name from parent class

南笙酒味 提交于 2019-12-07 03:02:15
问题 I'm trying to accomplish this without requiring a function on the child class... is this possible? I have a feeling it's not, but I really want to be sure... <?php class A { public static function who() { echo __CLASS__; } public static function test() { static::who(); // Here comes Late Static Bindings } } class B extends A { public static function who() { echo __CLASS__; } } B::test(); //returns B ?> 回答1: Use get_called_class() instead of __CLASS__ . You'll also be able to replace static

late static binding | without modifying parent class with `static` keyword

主宰稳场 提交于 2019-12-06 23:14:26
I have following parent and child class. class Parent_class { protected static function method_one() { echo "I am in Parent_class in method_one"; } protected function execute() { static::method_one(); } public function start() { $this->execute(); } } class Child_class extends Parent_class { protected static function method_one() { echo "I am in Child_class in method_one"; } } $obj = new Child_class(); $obj->start(); Result - it is calling Child class method. The result is as expected because of static late binding is supported in php5.3 with the already reserved keyword static . But the issue

static:: vs. self:: - are there any downsides?

隐身守侯 提交于 2019-12-06 00:42:04
In this StackOverflow question I learned that self:: was not inheritance-aware where static:: was (in PHP). When it comes to defining a bunch of constants within a class, if you want to override those constants in a subclass to change default "behaviours", it becomes necessary to use static:: so that a method on the parent class that references the constant, honours the "override". In the 2 years since I asked that original question, I have started using static:: extensively, to the point that I rarely use self:: since self:: would appear to limit the extensibility of a class that uses

PHPDoc and late (static or dynamic) binding

走远了吗. 提交于 2019-12-05 13:56:59
问题 Most PHP IDEs rely on phpdoc to get hints about the type of an expression. Yet, I use frequently this pattern, which doesn't seem to be covered: class Control { private $label = ''; /** @return ??? */ public static function Make(){ return new static(); } /** @return ??? */ public function WithLabel($value){ $this->label = $value; return $this; } /** @return void */ public function Render(){ /* ... */ } } class Textbox extends Control { private $text = ''; /** @return ??? */ public function

How to get child class name from parent class

风格不统一 提交于 2019-12-05 09:00:15
I'm trying to accomplish this without requiring a function on the child class... is this possible? I have a feeling it's not, but I really want to be sure... <?php class A { public static function who() { echo __CLASS__; } public static function test() { static::who(); // Here comes Late Static Bindings } } class B extends A { public static function who() { echo __CLASS__; } } B::test(); //returns B ?> Use get_called_class() instead of __CLASS__ . You'll also be able to replace static with self as the function will resolve the class through late binding for you: class A { public static

What is Call Forwarding and Static Calls in PHP or otherwise Late static binding? [closed]

不想你离开。 提交于 2019-12-05 02:28:20
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . One code sample I have got from a website, but it was difficult for me to understand the output. I am sharing the code : class A { public static function foo() { static::who(); } public static function who() { echo __CLASS__."\n"; } } class B extends A { public static function

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

会有一股神秘感。 提交于 2019-12-04 03:33:38
问题 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:

PHPDoc and late (static or dynamic) binding

孤人 提交于 2019-12-03 23:26:19
Most PHP IDEs rely on phpdoc to get hints about the type of an expression. Yet, I use frequently this pattern, which doesn't seem to be covered: class Control { private $label = ''; /** @return ??? */ public static function Make(){ return new static(); } /** @return ??? */ public function WithLabel($value){ $this->label = $value; return $this; } /** @return void */ public function Render(){ /* ... */ } } class Textbox extends Control { private $text = ''; /** @return ??? */ public function WithText($text){ $this->width = $text; return $this; } } Now I can use the classes like this: Textbox: