late-static-binding

late static binding in PHP

a 夏天 提交于 2020-01-01 06:18:22
问题 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

late static binding in PHP

…衆ロ難τιáo~ 提交于 2020-01-01 06:18:18
问题 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

late static binding in PHP

时光毁灭记忆、已成空白 提交于 2020-01-01 06:18:11
问题 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

Return type “self” in abstract PHP class

ぐ巨炮叔叔 提交于 2019-12-23 10:36:07
问题 PHP 7.1 I'm currently trying to make an abstract class to provide and define and partially implement functionality of its' child classes. Here I use the following construct: abstract class Parent { public static function fromDB(string $name = '') { $instance = new static(); if (!empty($name)) { $instance->setName($name)->read(); } return $instance; } public abstract function read(); public abstract function setName(string $name): self; } Here PHP seems to understand that setName($name)

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

醉酒当歌 提交于 2019-12-23 01:44:20
问题 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

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

倾然丶 夕夏残阳落幕 提交于 2019-12-23 01:44:04
问题 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

PHP 5.2 Equivalent to Late Static Binding (new static)?

强颜欢笑 提交于 2019-12-19 19:45:26
问题 I am trying to make a script that is built for php 5.3 work on a php 5.2 server. The script uses a lot of late static binding like: return new static($options); What is the equivalent to this in php 5.2? would it be new self somehow? Or is it not possible to achieve the same effect... Thanks EDIT: Here is a related question New self vs. new static Juts trying to wrap my head around this late static binding stuff... 回答1: I think the only way is to pass by a protected static method that build

How do I call a static child function from parent static function?

℡╲_俬逩灬. 提交于 2019-12-19 00:40:30
问题 How do I call child function from parent static function ? In php5.3 there is a built in method called get_called_class() to call child method from parent class. But my server is running with php 5.1 . Is there any way can do this ? I want to call it from a static function . So that I can not use "$this" So i should use "self" keyword. Below example my parent class is "Test123" , from the parent class static function "myfunc" am trying to call child class function like this "self::test();"

Is it possible to overuse late static binding in PHP?

最后都变了- 提交于 2019-12-18 13:08:43
问题 Starting with version 5.3, PHP supports late binding for static methods. While it's an undoubtedly useful feature, there are only several cases where its use is really necessary (e.g. the Active Record pattern). Consider these examples: 1. Convenience constructors ( ::create() ) class SimpleObject { public function __construct() { /* ... */ } public static function create() { return new static; // or: return new self; } } If this class may be extended (however, it's not extended by any class

Inherit static properties in subclass without redeclaration?

…衆ロ難τιáo~ 提交于 2019-12-18 07:34:25
问题 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