


Overloading of php __get __set isset unset __call __callStatic python static method import static import difference gcc static link static library
overloading in php is different from traditional object-oriented rewriting, such as in java:
<code>class A{ public void methodName(参数<span>1</span>); public void methodName(参数<span>1</span>,参数<span>2</span>); public void methodName(参数<span>1</span>,参数<span>2</span>,参数<span>3</span>); <span>...</span>}</code>
Overloading in php is an "elegant" error handling mechanism when calling non-existent properties or methods on a class or object. I don’t understand why this is also called overloading in PHP. It has nothing to do with overloading in traditional object-oriented programming.
PHP overloading relies on the magic methods __get() __set() isset() unset() __call() __callStatic(). When we access non-existent properties or methods, the system will automatically call these magic methods.
Code:
<code><span><span><?php</span> header(<span>"content-type:text/html;charset=utf-8"</span>); <span><span>class</span><span>Sample</span>{</span><span>public</span><span>$p1</span> = <span>1</span>;<span>//类中声明的属性p1</span><span>//保存被重载的数据</span><span>private</span><span>$data</span> = <span>array</span>(); <span>/* 在访问对象不存在的属性时__get被调用 $name:变量名 */</span><span><span>function</span><span>__get</span><span>(<span>$name</span>)</span> {</span><span>"<br>__get:: "</span>.<span>$name</span>; <span>if</span>(array_key_exists(<span>$name</span>, <span>$this</span>->data)){ <span>return</span><span>$this</span>->data[<span>$name</span>]; } <span>$trace</span> = debug_backtrace(); trigger_error( <span>'访问类中不存在的属性'</span>.<span>$name</span>. <span>' 文件:'</span>.<span>$trace</span>[<span>0</span>][<span>'file'</span>]. <span>' 所在行'</span>.<span>$trace</span>[<span>0</span>][<span>'line'</span>] ,E_USER_WARNING); <span>return</span><span>null</span>; } <span>/* 对象不存在的属性赋值时__set被调用 $name:变量名 $value:变量值 */</span><span><span>function</span><span>__set</span><span>(<span>$name</span>,<span>$value</span>)</span>{</span><span>echo</span><span>"<br> __set:: $name = $value "</span>; <span>$this</span>->data[<span>$name</span>] = <span>$value</span>; } <span>/* 对象不存在的属性使用isset()或empty() 时__isset被调用 $name:变量名 $value:变量值 */</span><span><span>function</span><span>__isset</span><span>(<span>$name</span>)</span>{</span><span>echo</span><span>"<br>isset:: $name "</span>; <span>return</span><span>isset</span>(<span>$this</span>->data[<span>$name</span>]); } <span>/* 对象不存在的属性使用unset()时被调用 $name:变量名 $value:变量值 */</span><span><span>function</span><span>__unset</span><span>(<span>$name</span>)</span>{</span><span>echo</span><span>"<br>__unset:: $name"</span>; <span>unset</span>(<span>$this</span>->data[<span>$name</span>]); } <span>/* 类的对象的不存在的实例方法调用的时候,会自动调用本方法,该方法必须是public */</span><span>public</span><span><span>function</span><span>__call</span><span>(<span>$name</span> , <span>$arguments</span>)</span>{</span><span>if</span>(<span>$name</span>===<span>'f1'</span>){ <span>echo</span><span>"<br>"</span>.<span>$name</span>.<span>"方法被调用,"</span>.<span>"参数:"</span>; var_dump(<span>$arguments</span>); }<span>else</span><span>if</span>(<span>$name</span>===<span>'f2'</span>){ <span>echo</span><span>"<br>"</span>.<span>$name</span>.<span>"方法被调用,"</span>.<span>"参数:"</span>; var_dump(<span>$arguments</span>); }<span>else</span>{ trigger_error(<span>"非法调用!"</span>,E_USER_WARNING); } } <span>/* 类的对象的不存在的静态方法调用的时候,会自动调用本方法 5.3.0 新增 __callStatic()魔术方法。可见性未设置为 public 或未声明为 static 的时候会产生一个警告 */</span><span>public</span><span>static</span><span><span>function</span><span>__callStatic</span><span>(<span>$name</span> , <span>$arguments</span>)</span>{</span><span>echo</span><span>"<br>"</span>.<span>$name</span>.<span>"静态方法被调用,"</span>.<span>"参数:"</span>; var_dump(<span>$arguments</span>); } } <span>$s</span> = <span>new</span> Sample(); <span>echo</span><span>"<br>访问类中存在的实例属性:s->p1:: "</span>.<span>$s</span>->p1; <span>//属性重载只能在对象中进行。在静态方法中,这些魔术方法将不会被调用。所以这些方法都不能被 声明为 static。从 PHP 5.3.0 起, 将这些魔术方法定义为 static 会产生一个警告。</span><span>//Fatal error: Access to undeclared static property: Sample::$p3</span><span>//echo "<br>访问类中不存在的静态属性不会调用魔术方法__get:".Sample::$p3;//错误</span><span>//访问类中不存在的属性,类中的魔术方法__get会被调用</span><span>echo</span><span>$s</span>->p2; <span>//给类中不存在的属性赋值,类中的魔术方法__set会被调用</span><span>$s</span>->p2 = <span>88</span>; <span>echo</span><span>'<br>'</span>.<span>$s</span>->p2; <span>//输出 88</span><span>//类中的魔术方法__isset会被调用</span> var_dump(<span>isset</span>(<span>$s</span>->p2)); <span>//类中的魔术方法__isset会被调用</span> var_dump(<span>empty</span>(<span>$s</span>->p2)); <span>//类中的魔术方法__unset会被调用</span><span>unset</span>(<span>$s</span>->p2); <span>echo</span><span>'<br>'</span>.<span>$s</span>->p2; <span>//p2被销毁,报错</span><span>$s</span>->f1(); <span>$s</span>->f1(<span>1</span>,<span>2</span>); <span>$s</span>->f1(<span>1</span>,<span>2</span>,<span>"hello"</span>); <span>$s</span>->f2(<span>true</span>,<span>"111"</span>); <span>$s</span>->f3(); <span>//调用对象不存在的静态方法</span> Sample::say(<span>'hello'</span>); Sample::say(<span>'hello'</span>,<span>'php'</span>); <span>?></span></span></code>
The above introduces the overloading of php __get __set isset unset __call __callStatic, including static content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The role and usage of static in C language: 1. Variable scope; 2. Life cycle; 3. Internal function; 4. Modify global variables; 5. Modify function; 6. Other uses; Detailed introduction: 1. Variable scope, when If there is the static keyword before a variable, then the scope of the variable is limited to the file in which it is declared. In other words, the variable is a "file-level scope", which is very useful for preventing the "duplicate definition" problem of variables; 2. Life cycle, static variables are initialized once when the program starts executing, and destroyed when the program ends, etc.

Practical application scenarios and usage skills of the static keyword in C language 1. Overview static is a keyword in C language, used to modify variables and functions. Its function is to change its life cycle and visibility during program running, making variables and functions static. This article will introduce the actual application scenarios and usage techniques of the static keyword, and illustrate it through specific code examples. 2. Static variables extend the life cycle of variables. Using the static keyword to modify local variables can extend their life cycle.

1. static Please look at the following program first: publicclassHello{publicstaticvoidmain(String[]args){//(1)System.out.println("Hello, world!");//(2)}} Have seen this Segment programs are familiar to most people who have studied Java. Even if you have not learned Java but have learned other high-level languages, such as C, you should be able to understand the meaning of this code. It simply outputs "Hello, world" and has no other use. However, it shows the main purpose of the static keyword.

Modifier abstract (abstract) 1. Abstract can modify a class (1) The class modified by abstract is called an abstract class (2) Syntax: abstractclass class name {} (3) Features: Abstract classes cannot create objects separately, but they can be declared Reference the abstract class name reference name; (4) Abstract classes can define member variables and member methods (5) Abstract classes have constructors. When used to create subclass objects, jvm creates a parent class object by default; abstract constructor methods apply Applied when jvm creates parent class object. 2. Abstract can modify methods (1) The method modified by asbtract is called an abstract method (2) Syntax: access modifier abstract return value

The functions of static: 1. Variables; 2. Methods; 3. Classes; 4. Other uses; 5. Multi-threaded environment; 6. Performance optimization; 7. Singleton mode; 8. Constants; 9. Local variables; 10. Memory Layout optimization; 11. Avoid repeated initialization; 12. Use in functions. Detailed introduction: 1. Variables, static variables. When a variable is declared as static, it belongs to the class level, not the instance level, which means that no matter how many objects are created, only one static variable exists, and all objects share this Static variables and so on.

Springboot reads the pro file and injects static static variables mailConfig.properties#Server mail.host=smtp.qq.com#Port number mail.port=587#Email account mail.userName=hzy_daybreak_lc@foxmail.com#Email authorization code mail.passWord =vxbkycyjkceocbdc#Time delay mail.timeout=25000#Sender mail.emailForm=hzy_daybreak_lc@foxmail.com#Sender mai

The "static" in php static static methods means that these properties and methods can be called directly without instantiating the class; static is a keyword used to modify the properties and methods of the class, and its usage syntax is such as "class Foo {public static $my_static = 'hello';}".

PHP is a popular open source server-side scripting language widely used in web development. The PHP language is not only easy to learn and use, but also supports a variety of programming paradigms, object-oriented programming, functional programming, etc. In PHP, there are some special syntax keywords, such as Static, Final, Abstract, etc. These keywords have special functions in object-oriented programming. This article will introduce these keywords in detail. Static keyword In PHP, the Static keyword has two uses
