This article introduces the object-oriented abstract method and abstract class __call clone object usage in PHP. Friends who need to know how to do it can learn it.
Abstract methods and abstract classes
In OOP language, a class can have one or more subclasses, and each class has at least one public method as an interface for external code to access it. Abstract methods are introduced to facilitate inheritance. Let's first take a look at the definitions of abstract classes and abstract methods before explaining their uses.
What is an abstract method? The method we define in the class without a method body is an abstract method. The so-called no method body means that when the method is declared, there are no curly brackets and its contents. Instead, a parentheses is added directly after the method name when declaring it. ends with the number, and when declaring an abstract method, add a keyword "abstract" to modify it; for example:
The code is as follows |
Copy code |
代码如下 |
复制代码 |
abstract function fun1();
abstract function fun2();
|
abstract function fun1();
abstract function fun2();
|
代码如下 |
复制代码 |
abstract class Demo
{
var $test;
abstract function fun1();
abstract function fun2();
function fun3()
{
... ...
}
}
|
The above example is the abstract methods "fun1()" and "fun2()" without a method body modified by "abstract". Don't forget that there is a semicolon after the abstract method; so what is an abstract class? As long as a method in a class is an abstract method, then the class must be defined as an abstract class, and the abstract class must also be modified with the "abstract" keyword; in an abstract class, there can be methods and member attributes that are not abstract, but as long as If a method is an abstract method, the class must be declared as an abstract class and decorated with "abstract". For example:
The code is as follows |
Copy code |
abstract class Demo
代码如下 |
复制代码 |
abstract class Demo
{
var $test;
abstract function fun1();
abstract function fun2();
function fun3()
{
... ...
}
}
|
{
var $test;
abstract function fun1();
abstract function fun2();
Function fun3()
{
... ...
}
}
|
In the above example, an abstract class "Demo" is defined and modified with "abstract". In this class, a member attribute "$test" and two abstract methods "fun1" and "fun2" are defined. A non-abstract method fun3(); So how do we use abstract classes? The most important point is that abstract classes cannot produce instance objects, so they cannot be used directly. We have mentioned many times that classes cannot be used directly. We are using objects instantiated through classes, so abstract classes cannot produce instance objects. What is the use of declaring an abstract class? We use abstract methods as templates for subclass overloading. Defining an abstract class is equivalent to defining a specification. This specification requires subclasses to comply. After the subclass succeeds the abstract class, it The abstract method is implemented according to the needs of the subclass. The subclass must implement all the abstract methods in the parent class. Otherwise, if there are still abstract methods in the subclass, then the subclass is still an abstract class and cannot be instantiated. Why do we have to inherit from the abstract class? Because sometimes if we want to implement some functions, we must inherit from an abstract class, otherwise you will not be able to implement these functions. If you inherit an abstract class, you must implement the abstract methods in the class;
The code is as follows |
Copy code |
abstract class Demo
{
var $test;
abstract function fun1();
abstract function fun2();
Function fun3()
{
... ...
}
}
|
//Abstract classes can generate instance objects, so this is wrong. The instantiated objects are handed over to subclasses
The code is as follows |
Copy code |
代码如下 |
复制代码 |
$demo=new Demo();
class Test extends Demo
{
function fun1()
{
... ...
}
function fun2()
{
... ...
}
}
|
$demo=new Demo();
class Test extends Demo
代码如下 |
复制代码 |
$test=new Test(); |
{
Function fun1()
{
... ...
}
function fun2()
{
... ...
}
代码如下 |
复制代码 |
//这是一个测试的类,里面没有属性和方法
class Test
{
}
//产生一个Test类的对象
$test=new Test();
//调用对象里不存在的方法
$test->demo("one", "two", "three");
//程序不会执行到这里
echo "this is a test";
|
}
|
//Subclasses can instantiate objects because they implement all abstract methods in the parent class
代码如下 |
复制代码 |
//这是一个测试的类,里面没有属性和方法
class Test
{
//调用不存的方法时自动调用的方法,第一个参数为方法名,第二个参数是数组参数
function __call($function_name, $args)
{
print "你所调用的函数:$function_name(参数:";
print_r($args);
print ")不存在!n";
}
}
//产生一个Test类的对象
$test=new Test();
//调用对象里不存在的方法
$test->demo("one", "two", "three");
//程序不会退出可以执行到这里
echo "this is a test";
|
__call handles calling errors
In program development, if when using an object to call an internal method of the object, the called method does not exist, then the program will error, and then the program will exit and cannot continue execution. So when the program calls a method that does not exist inside the object, can we be prompted that the method called and the parameters used do not exist, but the program can continue to execute. At this time, we have to use the method that is automatically called when calling the method that does not exist. "__call()".
The code is as follows |
Copy code |
//This is a test class, there are no attributes and methods in it
class Test
{
}
//Generate an object of Test class
$test=new Test();
//Call a method that does not exist in the object
$test->demo("one", "two", "three");
//The program will not execute here
echo "this is a test";
|
The following error occurred in the above example, and the program cannot continue to execute;
Fatal error: Call to undefined method Test::demo()
Below we add the "__call()" method. This method has 2 parameters. The first parameter is the process of calling a non-existent method. When the __call() method is automatically called, the non-existent method is The method name is passed to the first parameter, and the second parameter is passed in the multiple parameters of this method in the form of an array.
The code is as follows |
Copy code |
//This is a test class, there are no attributes and methods in it
class Test
{
//Method that is automatically called when calling a non-existing method. The first parameter is the method name, and the second parameter is the array parameter
Function __call($function_name, $args)
{
print "The function you called: $function_name(parameter: ";
print_r($args);
Print ") does not exist! N";
}
}
//Generate an object of Test class
$test=new Test();
//Call a method that does not exist in the object
$test->demo("one", "two", "three");
//The program will not exit and can be executed here
echo "this is a test";
|
The output result of the above example is:
The function you called: demo (parameter: Array ( [0] => one [1] => two [2] => three ) ) does not exist! this is a test.
Clone Object
Sometimes we need to use two or more identical objects in a project. If you use the "new" keyword to recreate the object and then assign the same attributes, this is more cumbersome and error-prone, so It is very necessary to completely clone an identical object from an object, and after cloning, the two objects will not interfere with each other.
In PHP5 we use the "clone" keyword to clone objects;
The code is as follows |
Copy code |
代码如下 |
复制代码 |
class Person
{
//下面是人的成员属性
var $name; //人的名子
var $sex; //人的性别
var $age; //人的年龄
//定义一个构造方法参数为属性姓名$name、性别$sex和年龄$age进行赋值
function __construct($name="", $sex="", $age="")
{
$this->name=$name;
$this->sex=$sex;
$this->age=$age;
}
//这个人可以说话的方法, 说出自己的属性
function say()
{
echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."";
}
}
$p1=new Person("张三", "男", 20);
//使用“clone”克隆新对象p2,和p1对象具有相同的属性和方法。
$p2=clone $p1;
$p2->say();
|
class Person
{
//The following are the member attributes of people
var $name; //The person’s name
var $sex; //Person’s gender
var $age; //The person’s age
//Define a constructor parameter to assign values to the attributes name $name, gender $sex and age $age
Function __construct($name="", $sex="", $age="")
{
$this->name=$name;
$this->sex=$sex;
$this->age=$age;
}
//This person can speak in a way that tells his own attributes
Function say()
{
echo "My name is: ".$this->name." Gender: ".$this->sex." My age is: ".$this->age."";
}
}
$p1=new Person("Zhang San", "Male", 20);
//Use "clone" to clone the new object p2, which has the same properties and methods as the p1 object.
$p2=clone $p1;
$p2->say();
|
PHP5 defines a special method name "__clone()" method, which is automatically called when an object is cloned. Using the "__clone()" method will create an object with the same attributes and methods as the original object. If If you want to change the contents of the original object after cloning, you need to rewrite the original properties and methods in __clone(). The "__clone()" method can have no parameters. It automatically contains two pointers, $this and $that, and $this points to copy, and $that points to the original;
The code is as follows
代码如下 |
复制代码 |
class Person
{
//下面是人的成员属性
var $name; //人的名子
var $sex; //人的性别
var $age; //人的年龄
//定义一个构造方法参数为属性姓名$name、性别$sex和年龄$age进行赋值
function __construct($name="", $sex="", $age="")
{
$this->name=$name;
$this->sex=$sex;
$this->age=$age;
}
//这个人可以说话的方法, 说出自己的属性
function say()
{
echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."";
}
//对象克隆时自动调用的方法, 如果想在克隆后改变原对象的内容,需要在__clone()中重写原本的属性和方法
function __clone()
{
//$this指的复本p2, 而$that是指向原本p1,这样就在本方法里,改变了复本的属性。
$this->name="我是假的$that->name";
$this->age=30;
}
}
$p1=new Person("张三", "男", 20);
$p2=clone $p1;
$p1->say();
$p2->say();
上例输出:
|
|
Copy code
|
class Person |
{
//The following are the member attributes of people
var $name; //The person’s name
var $sex; //Person’s gender
var $age; //The person’s age
//Define a constructor parameter to assign values to the attributes name $name, gender $sex and age $age
Function __construct($name="", $sex="", $age="")
{
$this->name=$name;
$this->sex=$sex;
$this->age=$age;
}
//This person can speak in a way that tells his own attributes
Function say()
{
echo "My name is: ".$this->name." Gender: ".$this->sex." My age is: ".$this->age."";
}
//Method automatically called when an object is cloned. If you want to change the content of the original object after cloning, you need to rewrite the original attributes and methods in __clone()
{
//$this points to the copy p2, and $that points to the original p1, so in this method, the attributes of the copy are changed.
$this->name="I am fake $that->name";
$this->age=30;
}
}
$p1=new Person("Zhang San", "Male", 20);
$p2=clone $p1;
$p1->say();
$p2->say();
Output of the above example:
My name is: Zhang San Gender: Male My age is: 20
My name is: I am the fake Zhang San Gender: Male My age is: 30
http://www.bkjia.com/PHPjc/629205.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629205.htmlTechArticleThis article introduces the object-oriented abstract method and abstract class __call clone object usage in PHP. If necessary, send Friends who understand can learn from it. Abstract methods and abstract classes In OOP language,...