PHP Object-oriented abstract method and abstract class __call cloning object detailed tutorial _php tutorial

Source: Internet
Author: User
This article introduces the object-oriented abstract methods in PHP and the use of abstract class __call cloning objects, and friends who need to be able to learn.

Abstract methods and abstract classes


In the OOP language, a class can have one or more subclasses, and each class has at least one public method that accesses its interface as an external code. and abstract methods are introduced for the convenience of inheritance, let us first look at the definition of abstract and abstract methods to illustrate its purpose.

What is an abstract method? The method that we define in our class is abstract, and the so-called no method body refers to the absence of curly braces and the contents of the method declaration, but rather the end of the method name with a semicolon at the time of declaration, and the addition of a keyword "abstract" when declaring an abstract method. For example

The code is as follows Copy Code

Abstract function fun1 ();
Abstract function fun2 ();

The above example is the abstract method that "abstract" modifies without the method body "fun1 ()" and "fun2 ()", do not forget that there is a semicolon behind the abstract method; What is an abstract class? As long as there is one method in a class that is abstract, the class is defined as an abstract class, and the abstract class is also decorated with the "abstract" keyword, and there can be no abstract method and member property in the abstract class, but as long as there is a method that is abstract, the class must be declared as an abstract class, using " Abstract "to modify. For example:

The code is as follows Copy Code

Abstract class Demo
{
var $test;

Abstract function fun1 ();
Abstract function fun2 ();
function Fun3 ()
{
... ...
}
}

The

Above example defines an abstract class that "Demo" uses "abstract" to decorate, in which a member attribute "$test" is defined, and two abstract methods "Fun1" and "fun2" have a non-abstract method fun3 (); So how do we use abstract classes? The most important point is that the abstract class can not produce an instance object, so it can not be used directly, we mentioned many times before the class can not be used directly, we use the object through the class instantiation, then the abstract class can not produce an instance object we declare the abstract class what is the use of it? We use the abstract method as a template for subclass overloading, and defining an abstract class is equivalent to defining a specification that requires subclasses to obey, and then the abstract method in the abstract class is implemented according to the needs of the subclass after the subclass class. Subclasses must implement all the abstract methods in the parent class, otherwise there are abstract methods in the subclass, then the subclass is abstract or not, and why should we inherit from the abstract class? Because sometimes we have to implement some functions must inherit from the abstract class, otherwise these functions you can not implement, if inherit the abstract class, it is necessary to implement the abstract method of the class;

The code is as follows Copy Code

Abstract class Demo
{
var $test;

Abstract function fun1 ();
Abstract function fun2 ();
function Fun3 ()
{
... ...
}
}

An abstract class can produce an instance object, so it is wrong to instantiate the object to the subclass

The code is as follows Copy Code

$demo =new demo ();

Class Test extends Demo
{
function Fun1 ()
{
... ...
}

function fun2 ()
{
... ...
}
}

Subclasses can instantiate an object because all abstract methods in the parent class are implemented

The code is as follows Copy Code
$test =new test ();

__call Handling Call Errors


In program development, if the calling method does not exist when invoking an object's internal method, the program will go wrong, and then the program exit cannot continue execution. So, when the program calls the method that does not exist inside the object, we are prompted to call the method and the parameters used do not exist, but the program can continue to execute, at this time we will use the method called when the non-existent method "__call ()".

The code is as follows Copy Code

This is a test class with no properties or methods.
Class Test
{
}

Produces an object of the test class
$test =new test ();

Methods that do not exist in the calling object
$test->demo ("One", "one", "one", "three");

The program will not execute here
echo "This is a test";

The above example has the following error, the program can not continue execution;
Fatal error:call to undefined method Test::d Emo ()

Here we add the "__call ()" method, this method has 2 parameters, the first parameter is called the non-existent method procedure, when the __call () method is called automatically, the method name of the non-existent method is passed to the first parameter, the second parameter is the method of the multiple parameters in the form of an array to pass in.

The code is as follows Copy Code

This is a test class with no properties or methods.
Class Test
{
Methods that are called automatically when an existing method is called, the first parameter is the method name, the second argument is the array parameter
function __call ($function _name, $args)
{
Print "The function you call: $function _name (parameter:";
Print_r ($args);
Print ") does not exist! n ";
}
}

Produces an object of the test class
$test =new test ();

Methods that do not exist in the calling object
$test->demo ("One", "one", "one", "three");

The program will not exit can be executed here
echo "This is a test";

The above example outputs the result:
The function You call: Demo (parameter: Array ([0] = + one [1] = [2] = three)) does not exist! This is a test.


Cloning objects


Sometimes we need to use two or more of the same objects in a project, if you re-create the object using the "new" keyword, then assign the same attributes, it is cumbersome and error-prone, so it is necessary to completely clone an object exactly as it is, And after cloning, two objects do not interfere with each other.

In PHP5 we use the "clone" keyword to clone the object;

code as follows copy code

class person
{
//The following is a member of the human attribute
var $name; The name of the person
Var $sex;//person's gender
Var $age;//person's age

//Define a constructor method parameter to assign a property 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 speaks his own properties
function say ()
{
echo "My name is called". $this->name. "Gender:". $this->sex. " My age is: ". $this->age.";
}
}

$p 1=new person ("Zhang San", "male", +);
Clone a new object using "Clone" P2, with the same properties and methods as the P1 object.
$p 2=clone $p 1;

$p 2->say ();

PHP5 defines a special method named "__clone ()" method, which is the method that is called automatically when the object is cloned, and the "__clone ()" method will establish an object that has the same properties and methods as the original object, and if you want to change the contents of the original object after cloning, you need to __clone () Overriding the original properties and methods, the "__clone ()" method can have no parameters, it automatically contains $this and $that two pointers, $this point to the replica, and $that point to the original;

The code is as follows Copy Code

Class Person
{
The following is the person's member property
var $name; The name of the man
var $sex; Man's Sex
var $age; The age of the person

Define a constructor method parameter to assign a property 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 speaks his own attributes
function Say ()
{
echo "My name is called:". $this->name. "Gender:" $this->sex. "My Age is:". $this->age. "";
}

The method that is called automatically when the object is cloned, if you want to change the contents of the original object after cloning, you need to rewrite the original properties and Methods in __clone ()
function __clone ()
{
$this refers to the replica P2, while $that is pointing to the original P1, so that in this method, the properties of the replica are changed.
$this->name= "I am a false $that->name";
$this->age=30;
}
}

$p 1=new person ("Zhang San", "male", 20);

$p 2=clone $p 1;
$p 1->say ();
$p 2->say ();

The above example outputs:

My name is called: Zhang San Sex: Male my age is: 20
My name is called: I am false zhang San Sex: Male my age is: 30

http://www.bkjia.com/PHPjc/629205.html www.bkjia.com true http://www.bkjia.com/PHPjc/629205.html techarticle This article introduces the object-oriented abstract methods in PHP and the use of abstract class __call cloning objects, and friends who need to be able to learn. Abstract methods and abstract classes in the OOP language, one ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.