What is __ method __ in php?
Let me clarify your doubt.. Show
talking about Class :: It is a your logical implementation.. object :: It is a instance of your class (instance ~ COPY ~ clone ~ ...) when you are using --NEW-- operator ---> you are creating an OBJECT (nothing but the COPY) OF YOUR CLASS. CLASS =============> ~NEW~ =======> OBJECT (New Copy of your class..) similarly FUNCTION ==========> ~NEW~ =======> METHOD (Belongs to Object not Class) Class is the program you are writing while object is the copy of your class that is executing.. class resides in ROM(permanent) where as object resides in RAM(temporary memory area..) & when you are declaring an function as static it belongs only to class not to object & you can call the same(static function) by using ClassName::staticFunctionName() That is why there is no existence of STATIC_METHOD Rather STATIC_FUNCTION !!! code example::
OUTPUT::
IN Your case as you are using METHOD IN static context it showing the class name if the it would be a non static(Simple Function) it would show only function name only.. Magic methods are special methods which override PHP's default's action when certain actions are performed on an object. Caution All methods names starting with The following method names are considered magical: __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __serialize(), __unserialize(), __toString(), __invoke(), __set_state(), __clone(), and __debugInfo(). Warning All magic methods, with the exception of __construct(), __destruct(), and
__clone(), must be declared as Warning If type declarations are used in the definition of a magic method, they must be identical to the signature described in this document. Otherwise, a fatal error is emitted. Prior to PHP 8.0.0, no diagnostic was emitted. However, __construct() and __destruct() must not declare a return type; otherwise a fatal error is emitted. __sleep() and __wakeup()public __sleep(): array public __wakeup(): void
serialize() checks if the class has a function with the magic name __sleep(). If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn't return
anything then
The intended use of __sleep() is to commit pending data or perform similar cleanup tasks. Also, the function is useful if a very large object doesn't need to be saved completely. Conversely, unserialize() checks for the presence of a function with the magic name __wakeup(). If present, this function can reconstruct any resources that the object may have. The intended use of __wakeup() is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks. Example #1 Sleep and wakeup
__construct($dsn, $username, $password) __serialize() and __unserialize()public __serialize(): array public __unserialize(array serialize() checks if the class has a function with the magic name __serialize(). If so, that function is executed prior to any serialization. It must construct and return an associative array of key/value pairs that represent the serialized form of the object. If no array is returned a TypeError will be thrown.
The intended use of __serialize() is to define a serialization-friendly arbitrary representation of the object. Elements of the array may correspond to properties of the object but that is not required. Conversely, unserialize() checks for the presence of a function with the magic name __unserialize(). If present, this function will be passed the restored array that was returned from __serialize(). It may then restore the properties of the object from that array as appropriate.
Example #2 Serialize and unserialize
__construct($dsn, $username, $password) __toString()public __toString(): string The __toString() method allows a class to decide how it will react when it is treated like a string. For example, what Warning As of PHP 8.0.0, the return value follows standard PHP type semantics, meaning it will be coerced into a string if possible and if strict typing is disabled. As of PHP 8.0.0, any class that contains a __toString() method will also implicitly implement the Stringable interface, and will thus pass type checks for that interface. Explicitly implementing the interface anyway is recommended. In PHP 7.4, the returned value must be a string, otherwise an Error is thrown. Prior to PHP 7.4.0, the returned value must be a
string, otherwise a fatal Warning It was not possible to throw an exception from within a __toString() method prior to PHP 7.4.0. Doing so will result in a fatal error. Example #3 Simple example
__construct($foo) The above example will output: __invoke()__invoke( The __invoke() method is called when a script tries to call an object as a function. Example #4 Using __invoke()
The above example will output: Example #5 Using __invoke()
__construct(string $key) The above example will output: Array ( [0] => Array ( [id] => 3 [first_name] => Alice [last_name] => Gustav ) [1] => Array ( [id] => 2 [first_name] => Bob [last_name] => Filipe ) [2] => Array ( [id] => 1 [first_name] => John [last_name] => Do ) ) Array ( [0] => Array ( [id] => 1 [first_name] => John [last_name] => Do ) [1] => Array ( [id] => 2 [first_name] => Bob [last_name] => Filipe ) [2] => Array ( [id] => 3 [first_name] => Alice [last_name] => Gustav ) ) __set_state()static __set_state(array This static method is called for classes exported by var_export(). The only parameter of this method is an array containing exported properties in the form Example #6 Using __set_state()
__set_state($an_array) The above example will output: string(60) "A::__set_state(array( 'var1' => 5, 'var2' => 'foo', ))" object(A)#2 (2) { ["var1"]=> int(5) ["var2"]=> string(3) "foo" }
__debugInfo()__debugInfo(): array This method is called by var_dump() when dumping an object to get the properties that should be shown. If the method isn't defined on an object, then all public, protected and private properties will be shown. Example #7 Using __debugInfo()
__construct($val) { The above example will output: object(C)#1 (1) { ["propSquared"]=> int(1764) } jon at webignition dot net ¶ 13 years ago
jsnell at e-normous dot com ¶ 13 years ago
__set_state($an_array)
ctamayo at sitecrafting dot com ¶ 2 years ago
?> kguest at php dot net ¶ 5 years ago
$bar = ''; jeffxlevy at gmail dot com ¶ 17 years ago
smiley at HELLOSPAMBOT dot chillerlan dot net ¶ 6 years ago
Exception; daniel dot peder at gmail dot com ¶ 4 years ago
$shop_name; rayRO ¶ 16 years ago
__call($funcname, $args = array()) { daan dot broekhof at gmail dot com ¶ 10 years ago
(array('My_ToStringFixer', 'errorHandler')); dhuseby domain getback tld com ¶ 14 years ago
__sleep() { Wesley ¶ 11 years ago
martin dot goldinger at netserver dot ch ¶ 17 years ago
ddavenport at newagedigital dot com ¶ 17 years ago
CryptedStorageClass extends SomeStupidStorageClass yanleech at gmail dot com ¶ 14 years ago
staff at pro-unreal dot de ¶ 9 years ago
B extends A { rudie-de-hotblocks at osu1 dot php dot net ¶ 13 years ago
mastabog at hotmail dot com ¶ 17 years ago
osbertv at yahoo dot com ¶ 10 years ago
B Anonymous ¶ 12 years ago
__construct() { Dérico Filho ¶ 15 years ago
Anonymous ¶ 12 years ago
__construct($x=0, $y=0) { michal dot kocarek at seznam dot cz ¶ 14 years ago
__construct() {// All the magic is in single line: docey ¶ 16 years ago
krisj1010 at gmail.com ¶ 17 years ago
Travis Swicegood ¶ 15 years ago
__call($method, $args) danillo dot paiva dot toledo at gmail dot com ¶ 9 years ago
ricasiano at gmail dot com ¶ 1 year ago
Bar Anonymous ¶ 14 years ago
a {
tom ¶ 12 years ago
B extends A {
What is __ function __ in PHP?__FUNCTION__ returns only the name of the function. while as __METHOD__ returns the name of the class alongwith the name of the function. class trick. { function doit()
What is __ LINE __ PHP?Name. Description. __LINE__ The current line number of the file.
What is __ call method in PHP?The __call() method is invoked automatically when a nonexisting method or an inaccessible method is called. Use the __call() method to wrap existing API into a class.
What is __ set in PHP?__set() is run when writing data to inaccessible properties. __get() is utilized for reading data from inaccessible properties.
|