note 126959 modified in language.oop5.late-static-bindings by danbrown

From: Date: Sun, 03 Apr 2022 20:52:04 +0000
Subject: note 126959 modified in language.oop5.late-static-bindings by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218001@lists.php.net to get a copy of this message
Simplest way to understand is to run this script: <?php class ParentClass { static $A = 'ParentVariable'; static function parentCall() { echo get_called_class() . ', self: ' . self::$A . "\n"; echo get_called_class() . ', static: ' . static::$A . "\n"; echo "---\n"; } } class ChildClass extends ParentClass { static $A = 'ChildVariable'; static function childCall() { echo get_called_class() . ', self: ' . self::$A . "\n"; echo get_called_class() . ', static: ' . static::$A . "\n"; echo get_called_class() . ', parent: ' . parent::$A . "\n"; echo "---\n"; } } echo "Late Static Bindings:\n"; ParentClass::parentCall(); ChildClass::parentCall(); ChildClass::childCall(); ?> ---- Output: Late Static Bindings: ParentClass, self: ParentVariable ParentClass, static: ParentVariable --- ChildClass, self: ParentVariable ChildClass, static: ChildVariable --- ChildClass, self: ChildVariable ChildClass, static: ChildVariable ChildClass, parent: ParentVariable --was-- Simplest way to understand is to run this script: class ParentClass { static $A = 'ParentVariable'; static function parentCall() { echo get_called_class() . ', self: ' . self::$A . "\n"; echo get_called_class() . ', static: ' . static::$A . "\n"; echo "---\n"; } } class ChildClass extends ParentClass { static $A = 'ChildVariable'; static function childCall() { echo get_called_class() . ', self: ' . self::$A . "\n"; echo get_called_class() . ', static: ' . static::$A . "\n"; echo get_called_class() . ', parent: ' . parent::$A . "\n"; echo "---\n"; } } echo "Late Static Bindings:\n"; ParentClass::parentCall(); ChildClass::parentCall(); ChildClass::childCall(); ---- Output: Late Static Bindings: ParentClass, self: ParentVariable ParentClass, static: ParentVariable --- ChildClass, self: ParentVariable ChildClass, static: ChildVariable --- ChildClass, self: ChildVariable ChildClass, static: ChildVariable ChildClass, parent: ParentVariable https://php.net/manual/en/language.oop5.late-static-bindings.php

« previous php.notes (#218001) next »