Previously on the object-oriented only rote-type, work so long, looking back to see is a turn of understanding, for everyone to see.
1.final
FINAL:PHP5 adds a final keyword. If the method in the parent class is declared final, the subclass cannot overwrite the method, and if a class is declared final, it cannot be inherited.
Copy Code code as follows:
Class baseclass{
Public Function test () {
Ehco "Test";
}
Final public Function moretest () {
echo "Moretest";
}
}
Class ChildClass extends baseclass{
Public Function moretest () {
echo "Moretest";
}
}
Produce Fatal Error:cannot Override final Method Baseclass::moretest ()
2.__tostring (recommended with PHP5.2 or later)
Copy Code code as follows:
Class person{
protected $name;
protected $email;
Public Function SetName ($name) {
$this->name = $name;
}
Public Function Setemail ($email) {
$this->email = $email;
}
Public Function __tostring () {
Return "$this->name < $this->email>";
}
}
$rasums = new Person;
$rasums->setname (' Test ');
$rasums->setemail (' test@qq.com ');
Print $rasums;
3. Interfaces and abstract classes
The function of an interface: you want to ensure that a class implements one or more methods according to a specific name, visibility, and prototype.
Requirements for interfaces:
all abstract methods in a class
Abstract method Money is not to be added abstract
Interface Abstract Method property is public
member property must be a constant
Cases:
Copy Code code as follows:
Interface childtest{
Public function childtest ();
}
Class Fathtest implements childtest1,childtest2{
Public Function childtest () {
Echo 1;
}
............
}
Abstract function: In fact, abstract classes and interface classes are part of the very similar, remember where to see such a word, abstract class to take out the part of the class, this sentence looks very funny, in fact it tells the truth of abstract class, the role of abstract class, when you find that many of your classes in a lot of ways you continue to write repeatedly, Then you can consider using an abstract class, you might say, "I'm not allowed to rewrite a class for every public class I instantiate a common class, call the same method," and here is what the abstract class does, but it saves you from instantiating this step, Makes you as handy as calling this class of methods directly, and you can overload this method.
Abstract Requirements:
at least one abstract method in a class
Abstract method Money must add abstract
Cases:
Copy Code code as follows:
Abstract class database{
Abstract public Function connect ();
Abstract public Function query ();
Abstract public function fetch ();
Abstract public Function close ();
}
Note: Abstract methods cannot be defined as private methods and cannot be defined as final methods because they need to be inherited.
4. Passing Object references
PHP4: All "=" is to create a copy
PHP5: A copy is created when other "=" is assigned except the object, and the object is a reference
5. Cloning objects
One
Aggregation class:
Introduction to __call Method:
__call is invoked when client code uses a method that is not defined in the class.
__call () accepts two parameters, one is the method name, and the other is passed to all parameters (including arrays) to invoke the method.
Any value returned by the __call () method is returned to the customer, as if the calling method is real
Cases:
Copy Code code as follows:
Class address{
protected $city;
protected $country;
Public Function setcity ($city) {$this->city = $city;}
Public Function getcity () {return $this->city;}
Public Function Setcountry ($country) {$this->country = $country;}
Public Function Getcountry () {return $this->country;}
}
Class person{
protected $name;
protected $address;
Shallow clones
Public Function __construct () {
$this->address = new address;
}
Public Function SetName ($name) {
$this->name = $name;
}
Public Function GetName () {
return $this->name;
}
Public Function __call ($method, $arguments) {
if (method_exists ($this->address, $method)) {
return Call_user_func_array (Array ($this->address, $method), $arguments);
}
}
Deep cloning
Public Function __clone () {
$this->address = Clone $this->address;
}
}
$test 1 = new person;
$test 2 = clone $test 1;
$test 1->setname (' testname1 ');
$test 1->setcity (' testcity1 ');
$test 2->setname (' testname2 ');
$test 2->setcity (' Testcity2 ');
echo $test 1->getname (). ' -'. $test 1->getcity (). " \ n ";
echo $test 2->getname (). ' -'. $test 2->getcity (). " \ n ";
Testname1-testcity2
Testname2-testcity2
6. Important Property Access (__set __get __isset __unset) __isset __unset5.1 only useful
Role: Intercept the requirements of the attribute, in order to improve the degree of separation, but also to implement __isset () and __unset (), so that when we use isset to detect attributes or unset () to delete attributes, to ensure that the class behavior is correct
Cases:
Copy Code code as follows:
Class person{
Protected $__data = Array (' email ', ' test ');
Public Function __get ($property) {
if (Isset ($this->__data[$property])) {
return $this->__data[$property];
}else{
return false;
}
}
Public Function __set ($property, $value) {
if (Isset ($this->__data[$property])) {
return $this->__data[$property] = $value;
}else{
return false;
}
}
Public Function __isset ($property) {
if (Isset ($this->__data[$property])) {
return true;
}else{
return false;
}
}
Public Function __unset ($property) {
if (Isset ($this->__data[$property])) {
Return unset ($this->__data[$property]);
}else{
return false;
}
}
}
$test = new Person;
$test->email= ' test ';
Var_dump ($test->email);
Note:
These two methods only catch missing attributes, and if you define a property for your class, PHP will not invoke __get () and __set () when accessing this property.
These two methods completely destroy the idea of any attribute inheritance. If there is a __get () method in the parent object, and you implement your own __get () method in the subclass, your object will not execute correctly because the __get () method of the parent class will never be invoked, and of course you can use Parent::__get () to resolve
Disadvantages:
The speed is relatively slow
Using a magic accessor method, it is not possible to use reflection classes, such as Phpdocumentor, to automate the documentation of your code
It cannot be used for static properties