Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the magic methods in the PHP class

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces what are the magic methods in the PHP class, which are introduced in great detail and have certain reference value. Friends who are interested must finish reading!

1. _ _ construct () and _ _ destruct ()

When called when the instance is created / destroyed, 0 or more parameters can be passed.

Class A {function A () {echo "build A";} function _ _ destruct () {echo "destroy A";}} $obj = new A (); / / unset ($obj)

Note:The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.

With regard to constructors, starting with PHP5.3.3, a method named after a class in a class defined in a particular namespace will no longer be considered a constructor. In a class without a namespace, it is still the same constructor as before. Such as:

Namespace Foo;class Bar {public function Bar () {/ / treated as constructor in PHP 5.3.0-5.3.2 / / treated as regular method as of PHP 5.3.3}}

If there is no namespace Foo;, then Bar () will also be treated as a constructor. In addition, if the following situation exists:

Function _ _ construct () {echo "construct A";} function A () {echo "build A";} function _ destruct () {echo "destroy A";}}

If it contains both _ _ construct () and a function with the same name as the class name, only _ _ construct () will be called.

2. _ _ call () and _ _ callStatic ()

This method is called when an attempt is made to call a method that does not exist. Two parameters, one is the method name, and the other is the parameter array of the called method.

Class MethodTest {public function _ call ($name, $arguments) {/ / Note: value of $name is case sensitive. Echo "Calling object method'$name'". Implode ('', $arguments). "

";} public static function _ callStatic ($name, $arguments) {/ / Note: value of $name is case sensitive. Echo" Calling static method'$name' ".implode (', $arguments)."

";} $obj = new MethodTest;$obj- > runTest ('in','object','context'); MethodTest::runTest (' in','static','context')

Where $arguments is passed in as an array. Running result:

Calling object method 'runTest' in object context

Calling static method 'runTest' in static context

Also note the scope of the function protected and private:

Class TestMagicCallMethod {public function foo () {echo _ _ METHOD__.PHP_EOL. "

";} public function _ _ call ($method, $args) {echo _ METHOD__.PHP_EOL."

"; if (method_exists ($this, $method)) {$this- > $method ();}} protected function bar () {echo _ METHOD__.PHP_EOL."

";} private function baz () {echo _ METHOD__.PHP_EOL."

";} $test = new TestMagicCallMethod (); $test- > foo (); / * Outputs: * TestMagicCallMethod::foo * / $test- > bar (); / * Outputs: * TestMagicCallMethod::__call * TestMagicCallMethod::bar * / $test- > baz (); / * * Outputs: * TestMagicCallMethod::__call * TestMagicCallMethod::baz * /

3.__get () and _ _ set ()

Called when an attempt is made to read a property of an object that does not exist.

Note: we can use this function to implement various operations similar to reflection in java.

Class Test {public function _ get ($key) {echo $key. "not exists";} public function _ set ($key,$value) {echo $key. "=". $value;}} $t = new Test (); echo $t-> name. "

"; $t-> name =" abc "

Output:

Name not exists

Name = abc

4. _ _ toString ()

This method is similar to java's toString () method, which is called back when we print the object directly, and the function must return a string.

Class Test {private $name = "abc"; private $age = 12; public function _ _ toString () {return "name: $this- > name, age: $this- > age";} $t = new Test (); echo $t

Output:

Name: abc, age: 12

These are all the contents of this article entitled "what are the Magic methods in the PHP Class?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report