Function of class 1 instantiate object 2 as the basic class of other classes, inherited! 3. Role of calling its static member class
1. instantiate an object
2. it is inherited as the base class of other classes!
3. call its static members
Two main functions: instantiate objects and inherit objects.
There are two special classes with a single function!
1, ** only objects can be instantiated and cannot be inherited. ** Final class
2. it can only be inherited and cannot be instantiated. Abstract class
Final class and final method
The final class is the last chain on the inheritance chain and cannot be any more child classes.
final class End{}class Test extends End{}
Running result: error PHP Fatal error: Class Test may not inherit from final class (End) in/usercode/file. php on line 7
Final method. the restriction method cannot be overwritten by the quilt class.
Running result: error PHP Fatal error: Cannot override final method AmParentt: end_func () in/usercode/file. php on line 11
Abstract class, abstract class
There is one more thing than a common class (Class constant, static attribute, static method, non-static attribute, non-static method)-abstract method, which requires that non-abstract subclass must implement abstract methods.
Abstract method: No {} is called a method declaration. Parameters are supported.
abstract public function abstract_func($arg1,$arg2)
// The object cannot be instantiated.
Running result: PHP Fatal error: Cannot instantiate abstract class AmParent in/usercode/file. php on line 5
Only inherited
Two inherited situations:
1. subclass is an abstract class
2. if the subclass is not an abstract class, all abstract methods of the parent class must be implemented (with the method body {}).
Running result, error: PHP Fatal error: Class AmChild contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (AmParent: abstract_func) in/usercode/file. php on line 6
No error is reported:
1. add abstract to become an abstract class.
2. implement abstract methods
abstract_func( 'have implemented ',' abstract_func ');
Running result, error: have implemented effecact_func
Usage significance
Abstract class, which specifies that all non-abstract subclass methods must exist. the same name but the method body can be different.
';}} Class Mobiles extends Goods {public function sayName () {echo' I'm the name of a mobile phone
';}}$ Book1 = new Books; $ book1-> sayName (); $ mobile1 = new Mobiles; $ mobile1-> sayName ();
Running result: I am the name of a book, and I am the name of a mobile phone.
Interface Structure:
Restrict the public methods (interface methods) that a class (object) should have )!
The interface can only contain public abstract methods (incomplete methods) and constants!
Implement implements
Classes that implement the interface structure include the abstract methods defined in the interface.
Two options: 1 defined as an abstract class and 2 implemented interface methods!
// 1 is defined as abstract class Books implements iGoods {abstract public function sayName (); abstract public function sayPrice ();}
// 2 implementation interface method class Books implements iGoods {public function sayName () {echo 'I implemented the interface method';} public function sayPrice () {echo 'interface method I implemented ';}}
Compare the differences between abstract classes and interfaces:
1. an abstract class and a common class are inherited.
The normal class inherits the abstract class. First, you can obtain the existing regular members in the abstract class,
The second is to implement the abstract method (not necessarily public)
2. there is an implementation relationship between interfaces and common classes!
A common class implements an interface and can only implement public methods that are not implemented!
3. the interface is only used to define public methods and constants! Abstract classes can have everything!
4. multiple interfaces are implemented. One class can implement multiple interfaces at the same time
Tip:
An interface is an independent structure that is used to restrict the structure of a class!
// Use class_exists (@ param string) to determine whether the class has var_dump (class_exists ('C'); var_dump (class_exists ('A '));
Running result: bool (true) bool (false)
There are all public methods and constants without method bodies or abstract keywords.
The above is the php object-oriented syntax 5 final class, abstract class, Interface Structure (not class) content. For more information, please follow the PHP Chinese network (www.php1.cn )!