In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "examples of the use of magic methods in php". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "examples of the use of magic methods in php".
The copy code is as follows:
/ * PHP treats all class methods that begin with _ _ (two underscores) as magic methods. So when you define your own class methods, don't prefix them with _ _. * * /
/ / _ _ toString, _ _ set, _ _ get__isset (), _ _ unset ()
/ *
The _ _ toString method allows a class to decide how it will react when it is converted to a string.
_ _ set () is run when writing data to inaccessible members.
_ _ get () is utilized for reading data from inaccessible members.
_ _ isset () is triggered by calling isset () or empty () on inaccessible members.
_ _ unset () is invoked when unset () is used on inaccessible members.
, /
Class TestClass {
Private $data = array ()
Public $foo
Public function _ _ construct ($foo) {
$this- > foo = $foo
}
Public function _ toString () {
Return $this- > foo
}
Public function _ _ set ($name, $value) {
Echo "_ _ set, Setting'$name' to'$value'\ n"
$this- > data [$name] = $value
}
Public function _ _ get ($name) {
Echo "_ _ get, Getting'$name'\ n"
If (array_key_exists ($name, $this- > data)) {
Return $this- > data [$name]
}
}
/ * * As of PHP 5.1.0 * /
Public function _ _ isset ($name) {
Echo "_ _ isset, Is'$name' set?\ n"
Return isset ($this- > data [$name])
}
/ * * As of PHP 5.1.0 * /
Public function _ _ unset ($name) {
Echo "_ _ unset, Unsetting'$name'\ n"
Unset ($this- > data [$name])
}
}
$obj = new TestClass ('Hello')
Echo "_ _ toString, $obj\ n"
$obj- > a = 1
Echo $obj- > a. "\ n\ n"
Var_dump (isset ($obj- > a))
Unset ($obj- > a)
Var_dump (isset ($obj- > a))
Echo "\ n\ n"
/ * *
The output is as follows:
_ _ toString, Hello
_ _ set, Setting'a'to'1'
_ _ get, Getting 'a'
_ _ isset, Is 'a'set?
Bool (true)
_ _ unset, Unsetting 'a'
_ _ isset, Is 'a'set?
Bool (false)
* * /
/ / _ _ call _ _ callStatic
/ *
Mixed _ _ call (string $name, array $arguments)
Mixed _ _ callStatic (string $name, array $arguments)
_ _ call () is triggered when invoking inaccessible methods in an object context.
_ _ callStatic () is triggered when invoking inaccessible methods ina 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.
, /
Class MethodTest {
Public function _ _ call ($name, $arguments) {
/ / Note: value of $name is case sensitive.
Echo "_ _ call, Calling object method'$name'". Implode (',', $arguments). "\ n"
}
/ * * As of PHP 5.3.0 * /
Public static function _ _ callStatic ($name, $arguments) {
/ / Note: value of $name is case sensitive.
Echo "_ _ callStatic, Calling static method'$name'". Implode (',', $arguments). "\ n"
}
}
$obj = new MethodTest
$obj- > runTest ('in object context', 'param2',' param3')
/ / MethodTest::runTest ('in static context'); / / As of PHP 5.3.0
Echo "\ n\ n"
/ * *
The output is as follows:
_ _ call, Calling object method 'runTest' in object context, param2, param3
String (10) "_ _ invoke:"
, /
/ / _ _ invoke
/ *
The _ _ invoke method is called when a script tries to call an object as a function.
Note: This feature is available since PHP 5.3.0.
, /
Class CallableClass {
Function _ _ invoke ($x) {
Var_dump ($x)
}
}
$obj = new CallableClass
/ / $obj (5)
Var_dump ('_ _ invoke:'. Is_callable ($obj))
Echo "\ n\ n"
/ / _ _ sleep _ _ wakeup
/ *
Serialized serialize can convert variables including objects into continuous bytes data. You can store serialized variables in a file or transfer them over the network.
And then act in the line to restore to the original data. PHP can successfully store the properties and methods of the objects of the classes that you define before you play the objects of the class.
Sometimes you may need an object to execute immediately after acting. For this purpose, PHP automatically looks for _ _ sleep and _ _ wakeup methods.
When an object is serialized, PHP calls the _ _ sleep method (if it exists). After performing an object, PHP calls the _ _ wakeup method.
Neither of these methods accepts parameters. The _ _ sleep method must return an array of properties that need to be serialized. PHP discards the values of other properties.
If there is no _ _ sleep method, PHP will save all properties. The following example shows how to serialize an object using the _ _ sleep and _ _ wakeup methods.
The Id property is a temporary property that is not intended to remain in the object. The _ _ sleep method guarantees that the id property is not included in the serialized object.
When the actor makes a User object, the _ _ wakeup method establishes the new value of the id property. This example is designed to be self-preserving.
In actual development, you may find that objects that contain resources, such as images or data streams, need these methods
, /
Class User {
Public $name
Public $id
Function _ construct () {
/ / give user a unique ID assigns a different ID
$this- > id = uniqid ()
}
/ / _ _ sleep the type of return value is an array, and the value in the array is a field id that does not need to be serialized
Function _ sleep () {
/ / do not serialize this- > id does not serialize id
Return (array ("name"))
}
Function _ wakeup () {
/ / give user a unique ID
$this- > id = uniqid ()
}
}
/ / create object set up a device
$u = new User
$u-> name = "Leon"; / / serialize it serialization pay attention to not serializing the id attribute, the value of id is discarded
$s = serialize ($u)
Echo "_ _ sleep, _ _ wakeup, s: $s"; / / unserialize it actor id is reassigned
$U2 = unserialize ($s); / / $u and $U2 have different IDs $u and $U2 different ID
Print_r ($u)
Print_r ($U2)
Echo "\ n\ n"
/ * *
The output is as follows:
_ _ sleep, _ _ wakeup, s: User 4: "User": 1: {Spurl 4: "name"; SRAR4: "Leon";}
User Object
(
[name] = > Leon
[id] = > 4db1b17640da1
)
User Object
(
[name] = > Leon
[id] = > 4db1b17640dbc
)
, /
/ / _ _ set_state
/ *
This static method is called for classes exported by var_export () since PHP 5.1.0.
The only parameter of this method is an array containing exported properties in the form array ('property' = > value,...).
, /
Class A {
Public $var1
Public $var2
Public static function _ _ set_state ($an_array) {/ / As of PHP 5.1.0
/ / $an_array prints out an array, not the object passed on the call
Print_r ($an_array)
$obj = new A
$obj- > var1 = $an_array ['var1']
$obj- > var2 = $an_array ['var2']
Return $obj
}
}
$a = new A
$a-> var1 = 5
$a-> var2 = 'foo'
Echo "_ _ set_state:\ n"
Eval ('$b ='. Var_export ($a, true). ';')
/ / $b = A::__set_state (array (
/ / 'var1' = > 5
/ / 'var2' = >' foo'
/ /))
Var_dump ($b)
Echo "\ n\ n"
/ * *
The output is as follows:
_ _ set_state:
Array
(
[var1] = > 5
[var2] = > foo
)
Object (A) # 5 (2) {
["var1"] = >
Int (5)
["var2"] = >
String (3) "foo"
}
, /
/ / _ _ clone
Class SubObject {
Static $instances = 0
Public $instance
Public function _ construct () {
$this- > instance = + + self::$instances
}
Public function _ clone () {
$this- > instance = + + self::$instances
}
}
Class MyCloneable {
Public $object1
Public $object2
Function _ clone () {
/ / Force a copy of this- > object, otherwise
/ / it will point to same object.
$this- > object1 = clone $this- > object1
}
}
$obj = new MyCloneable ()
$obj- > object1 = new SubObject ()
$obj- > object2 = new SubObject ()
$obj2 = clone $obj
Print ("_ _ clone, Original Object:\ n")
Print_r ($obj)
Print ("_ _ clone, Cloned Object:\ n")
Print_r ($obj2)
Echo "\ n\ n"
/ * *
The output is as follows:
_ _ clone, Original Object:
MyCloneable Object
(
[object1] = > SubObject Object
(
[instance] = > 1
) [object2] = > SubObject Object
(
[instance] = > 2
))
_ _ clone, Cloned Object:
MyCloneable Object
(
[object1] = > SubObject Object
(
[instance] = > 3
) [object2] = > SubObject Object
(
[instance] = > 2
))
, /
At this point, I believe you have a deeper understanding of the "examples of the use of magic methods in php". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.