It's been a long time coming out of work, but there's a lot of experience in the project, but when it comes to the bottom of things, it's often silent. Now the bottom of the project design is less and fewer, can be said to be really used is that a little bit, the real core of things have been encapsulated by the framework. Always feel that they have been progressing very slowly, on the underlying design ideas, often also sensed. With the passage of time, we often feel that when the book to hate less, the next must be a bad complement design ideas.
Let's review class inheritance today.
Class inheritance is very important, as a programmer is also the basic daily deal with him, but there are some things you really know? The following is seen from the official website.
A class can use extends in a declaration, and the keyword inherits the methods and properties of another class. PHP does not support multiple inheritance, a class can inherit only one base class.
Inherited methods and properties can be overridden by a declaration with the same name. However, if the parent class defines a method that uses final, then the method cannot be overwritten. You can access the overridden method or property through Parent::.
When overriding a method, the parameters must be consistent or PHP will issue a e_strict level error message. However, with the exception of constructors, constructors can use different parameters when overridden.
I think everyone will know about the basic concept, but for final and parent: In the project I really did not how to use, ashamed ah.
The following is mainly about the use of these two keywords.
Final key word
A new keyword in PHP 5 that subclasses cannot overwrite if the method in the parent class is declared final. Similarly, if a class is declared final, it cannot be inherited.
It should be noted that attributes cannot be defined as final, and only classes and methods can be defined as final.
Range resolution operator (::)
The range resolution operator, or more simply a pair of colons, can be used to access static members, class constants, and also to override properties and methods in a class.
Self,parent and static These three special keywords are used to access the properties or methods within the class definition.
When a subclass overwrites a method in its parent class, PHP does not call a method that has been overridden in the parent class. The method of calling the parent class depends on the subclass. This mechanism is also used for constructors and destructors, overloads, and Magic methods.
The following is an example of a method that invokes a parent class:
Copy Code code as follows:
<?php
Class MyClass
{
protected function MyFunc () {
echo "Myclass::myfunc () \ n";
}
}
Class Otherclass extends MyClass
{
Overrides the definition of the parent class
Public Function MyFunc ()
{
But you can still call the overridden method in the parent class
Parent::myfunc ();
echo "Otherclass::myfunc () \ n";
}
}
$class = new Otherclass ();
$class->myfunc ();
/**
* Output Results->
* MYCLASS::MYFUNC ()
* OTHERCLASS::MYFUNC ()
*/
?>
Use the class name when referencing these items outside of the class definition.
:: Class from PHP 5.5, keyword class can also be used for the resolution of the name. Using Classname::class you can get a string that contains the fully qualified name of the class ClassName. This is especially useful for classes that use namespaces. These may be the topics to be discussed in the future, the concept that is to be discussed later, now we are still using php5.3.
But to be honest, there are some keywords that are really hard to use in small projects, but I'd like to know more about what's always needed. Especially when you're using a third party class library, when you look at his program logic, you can always learn something. Although some things you do not use for a long time, may forget, but after all he has passed in your mind, always for the future of life leave traces.
Finally the nonsense said, sharing a range of analytic operators (::) The application of the code, you also come to feel:
Copy Code code as follows:
<?php
Class CA
{
/**
* Default values for test properties that are used directly
*/
protected static $item = ' Foo ';
/**
* Default values for test properties used indirectly
*/
protected static $other = ' CA ';
public static Function method ()
{
Print Self:: $item. " \ r \ n ";
Print Self:: $other. " \ r \ n ";
}
public static function Setother ($val)
{
self:: $other = $val;
}
}
Class CB extends CA
{
/**
* Redefine default values for test properties
*/
protected static $item = ' Bar ';
public static function Setother ($val)
{
self:: $other = $val;
}
/**
* Do not re-declare method () methods
*/
}
Class CC extends CA
{
/**
* Redefine default values for test properties
*/
protected static $item = ' Tango ';
public static function method ()
{
Print self:: $item. " \ r \ n ";
Print self:: $other. " \ r \ n ";
}
/**
* Do not re-declare the Setother () method
*/
}
Class CD extends CA
{
/**
* Redefine default values for test properties
*/
protected static $item = ' Foxtrot ';
/**
* Do not re-declare any method to achieve the above process
*/
}
Cb::setother (' CB '); Cb::method ()!
Cb::method (); Ca::method ()!
Cc::setother (' CC '); Ca::method ()!
Cc::method (); Cc::method ()!
Cd::setother (' CD '); Ca::method ()!
Cd::method (); Ca::method ()!
/**
* Output Results->
* Foo
* CB
* Tango
* CC
* Foo
* CD
*/
?>
PHP extends class inheritance code example:
Copy Code code as follows:
< PHP
Class a{
public $x;
Public $y;
function __construct ($x =0, $y =0) {
$this->x= $x;
$this->y= $y;
}
function Getx () {
return $this->x;
}
function Gety () {
return $this->y;
}
function __destruct () {}
}
Class A2 extends a{}
/*extends is an inheritance function * *
$b 2=new A2 (10,10);
echo $b 2->getx (). <br> ";
echo $b 2->gety ();
?>
The content described above is the full implementation step of the PHP extends class inheritance.