php 戏法方法
php 魔术方法
__construct()__set()__get()
__isset()__unset()
__autoload()
__call()
__clone()
__invoke()
__sleep()
__wakeup()
__construct()
构造方法: 在PHP中的构造方法要求不能进行构造方法的重载,即构造 方法只有一个.
?
function __construct($name="宋", $sex="", $age=1) { //构造方法在对象诞生时为成员属性赋初值 $this->name=$name; $this->sex=$sex; $this->age=$age;}
?说明:
?
? ? 1. 在一个类中,它只可能有一个构造方法.
? ? 2. 所默认的构造方法是public的,如果使用private的话,则会构成单例模式.
?
一般来说,总是把类的属性定义为private,这更符合现实的逻辑。但是,对属性的读取和赋值操作是非常频繁的,因此在PHP5中,预定义了两个函数“__get()”和“__set()”来获取和赋值其属性,以及检查属性的“__isset()”和删除属性的方法“__unset()”。
//__get()方法用来获取私有属性private function __get($property_name){ if(isset($this->$property_name)) { return($this->$property_name); }else { return(NULL); } }//__set()方法用来设置私有属性private function __set($property_name,$value){ $this->$property_name=$value;}
<?phpclass Person{ //下面是人的成员属性, 都是封装的私有成员 private $name; //人的名子 private $sex; //人的性别 private $age; //人的年龄 //__get()方法用来获取私有属性 private function __get($property_name) { echo "在直接获取私有属性值的时候,自动调用了这个__get()方法<br>"; if (isset($this->$property_name)) { return ($this->$property_name); } else { return (NULL); } } //__set()方法用来设置私有属性 private function __set($property_name,$value) { echo"在直接设置私有属性值的时候,自动调用了这个__set()方法为私有属性赋值<br>"; $this->$property_name=$value; }}$p1=new Person();//直接为私有属性赋值的操作, 会自动调用__set()方法进行赋值$p1->name="张三";$p1->sex="男";$p1->age=20;//直接获取私有属性的值, 会自动调用__get()方法,返回成员属性的值echo"姓名:".$p1->name."<br>";echo"性别:".$p1->sex."<br>";echo"年龄:".$p1->age."<br>";?>
在直接设置私有属性值的时候,自动调用了这个__set()方法为私有属性赋值
在直接设置私有属性值的时候,自动调用了这个__set()方法为私有属性赋值
在直接获取私有属性值的时候,自动调用了这个__get()方法
姓名:张三
在直接获取私有属性值的时候,自动调用了这个__get()方法
性别:男
在直接获取私有属性值的时候,自动调用了这个__get()方法
年龄:20
private function __isset($nm){ echo"当在类外部使用isset()函数测定私有成员$nm时,自动调用<br>"; return isset($this->$nm);}
private function __unset($nm){ echo"当在类外部使用unset()函数来删除私有成员时自动调用的<br>"; unset($this->$nm);}
<?phpclass Person{ //下面是人的成员属性 private $name; //人的名子 private $sex; //人的性别 private $age; //人的年龄 //__get()方法用来获取私有属性 private function __get($property_name) { if (isset($this->$property_name)) { return ($this->$property_name); } else { return (NULL); } } //__set()方法用来设置私有属性 private function __set($property_name, $value) { $this->$property_name = $value; } //__isset()方法 private function __isset($nm) { echo "isset()函数测定私有成员时,自动调用<br>"; return isset($this->$nm); } //__unset()方法 private function __unset($nm) { echo "当在类外部使用unset()函数来删除私有成员时自动调用的<br>"; unset($this->$nm); }}$p1 = new Person();$p1->name = "this is a person name";echo var_dump(isset($p1->name)) . "<br>";echo $p1->name . "<br>";unset($p1->name);echo $p1->name;?>
<?phpinclude_once "cls/clsA.php";include_once "cls/clsB.php";$obj_A = new clsA();$obj_B = new clsB();?>
2:实例化类。?
<?php $obj_A = new clsA(); $obj_B = new clsB(); function __autoload($className){ include_once "cls/$className.php"; } ?>
2:调用__autoload函数,将伪实例的类名传入?
3:使用__autoload函数中,预先写好的加载规则进行加载类文件?
4:实例化对象(真实实例)?
因此,我们可以看出,对于PHP5的autoload函数,必须给定规则,否则一点用没有。
1:__autoload函数是用在类外面,而不是在类里面的函数。(__autoload也是被PHP5保护的关键字之一)?
2:完成对__autoload函数加载规则的编码。?
如上,当知道A是在cls目录中,而B是在cls/cls目录中。则编写__autoload加载规则就是必要的。
<?php// PHP5 Used __autoload function $obj_A = new clsA(); // in "cls" directory! $obj_B = new clsB(); // in "cls/cls" directory! function __autoload($className){ if (strtolowwer($className) == "clsb") { require_once "cls/cls/$className.php"; } else { include_once "cls/$className.php"; }}?>
<?phpclass TestClass{ public $foo; public function __construct($foo) { $this->foo = $foo; } //定义一个__toString方法,返加一个成员属性$foo public function __toString() { return $this->foo; }}$class = new TestClass('Hello');//直接输出对象Cheap Sunglassesecho $class;?>
__call( $method, $arg_array ) 当调用一个未定义的方法是调用此访求这里的未定义的方法包括没有权限访问的方法
?
<?php//当试图调用类中一个不存在或者不可用的方法时,//会执行该类中的__call()__call()必须接受两个参数,//第一个参数存放方法名称,//第二个参数存放不存在的方法的参数(此参数会放在与该参数同名的数组中)class callclass{ function __call($method_name, $p) { echo "使用__call尝试调用一个不存在/不可用的成员方法<br>"; echo $method_name; echo "<pre class="brush:php;toolbar:false">"; print_r($p); echo "
?
method
Array
(
? ? [0] => 1
? ? [1] => 2
? ? [2] => Hello
? ? [3] => HP
)
?
?

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

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

This article will explain in detail how PHP determines whether a specified key exists in an array. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP determines whether a specified key exists in an array: In PHP, there are many ways to determine whether a specified key exists in an array: 1. Use the isset() function: isset($array["key"]) This function returns a Boolean value, true if the specified key exists, false otherwise. 2. Use array_key_exists() function: array_key_exists("key",$arr

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table
