Hướng dẫn php method overloading
Overloading in PHP provides means to dynamically create properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types. The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope. The rest of this section will use the terms inaccessible properties and inaccessible methods to refer to this combination of declaration and visibility. All overloading methods must be defined as
Property overloadingpublic __set(string public __get(string public __isset(string public
__unset(string __set() is run when writing data to inaccessible (protected or private) or non-existing properties. __get() is utilized for reading data from inaccessible (protected or private) or non-existing properties. __isset() is triggered by calling isset() or empty() on inaccessible (protected or private) or non-existing properties. __unset() is invoked when unset() is used on inaccessible (protected or private) or non-existing properties. The $name argument is the name of the property being interacted with. The __set() method's $value argument specifies the value the $name'ed property should be set to. Property overloading only works in object context. These magic methods will not be triggered in static context. Therefore these methods should not be declared static. A warning is issued if one
of the magic overloading methods is declared
Example #1 Overloading properties via the __get(), __set(), __isset() and __unset() methods
__set($name, $value) The above example will output: Setting 'a' to '1' Getting 'a' 1 Is 'a' set? bool(true) Unsetting 'a' Is 'a' set? bool(false) 1 Let's experiment with the private property named 'hidden': Privates are visible inside the class, so __get() not used... 2 Privates not visible outside of class, so __get() is used... Getting 'hidden' Notice: Undefined property via __get(): hidden in Method overloadingpublic
__call(string public static __callStatic(string __call() is triggered when invoking inaccessible methods in an object context. __callStatic() is triggered when invoking inaccessible methods in a static context. The $name argument is the name of the method being called. The $arguments argument is an enumerated array containing the parameters passed to the $name'ed method. Example #2 Overloading methods via the __call() and __callStatic() methods
__callStatic($name, $arguments) The above example will output: Calling object method 'runTest' in object context Calling static method 'runTest' in static context theaceofthespade at gmail dot com ¶ 10 years ago
Anonymous ¶ 6 years ago
$UnexpectedBehaviour->EveryPosibleMethodCallAllowed(true, 'Why Not?');// And sure, why not use the most illegal property names you can think off egingell at sisna dot com ¶ 15 years ago
foo($a, $b) { pogregoire##live.fr ¶ 6 years ago
Anonymous ¶ 7 years ago
cottton at i-stats dot net ¶ 8 years ago
Ant P. ¶ 13 years ago
gabe at fijiwebdesign dot com ¶ 7 years ago
$property1; navarr at gtaero dot net ¶ 12 years ago
jstubbs at work-at dot co dot jp ¶ 16 years ago
justmyoponion at gmail dot com ¶ 2 years ago
PHP at jyopp dotKomm ¶ 16 years ago
__construct($object = null) { php at lanar dot com dot au ¶ 12 years ago
matthijs at yourmediafactory dot com ¶ 14 years ago
__call ($member, $arguments) { alexandre at nospam dot gaigalas dot net ¶ 15 years ago
timshaw at mail dot NOSPAMusa dot com ¶ 14 years ago
Nanhe Kumar ¶ 8 years ago
$_name; Adeel Khan ¶ 15 years ago
daevid at daevid dot com ¶ 13 years ago
'checking for '.implode(', ',$parts)." and suggesting the following: Daniel Smith ¶ 11 years ago
__call($method, $args) dans at dansheps dot com ¶ 11 years ago
__get($variable)
Marius ¶ 17 years ago
strata_ranger at hotmail dot com ¶ 12 years ago
var_dump($foo);// Won't call __set() because 'bar' is now declared php at sleep is the enemy dot co dot uk ¶ 15 years ago
$values = array(); johannes dot kingma at gmail dot com ¶ 11 months ago
__get( $property ) { turabgarip at gmail dot com ¶ 1 year ago
__construct() { DevilDude at darkmaker dot com ¶ 17 years ago
|