<?php /******************************************* Class object Instance description (normal Class) *******************************************/ Class classdemo{ Public $PublicVar;
private $PrivateVar;//external variable cannot be invoked
protected $ProtectedVar;//protected variable external and subclass inaccessible
public static $StaticVar =0;//static static variables static methods cannot access non-static methods and variables, non-static methods and variables can access static-too methods and variables
Const constvar= ';//When you have an attribute that you do not want to be modified, consider using const as a constant, using the class name:: Constant name; Interface Name:: constant /* 1. Constants need to be assigned an initial value when defined 2. Constants cannot be modified. 3. Constant names cannot have $, typically uppercase, default is public 4. Constants Use the self:: the name of a constant in the class:: Name of the constant */ Public function __construct (/* $name//) {//This can take parameters, with parameters must be created with parameters, or there will be errors $this->publicvar= $name; Self:: $StaticVar ++;//static Variable internal access method external access Method object name:: Variable name (classdemo:: $StaticVar) $this->publicvar= ' $PublicVar '; $this->privatevar= ' $PrivateVar '; $this->protectedvar= ' $ProtectedVar '; Self:: $StaticVar + +; echo "Classdemo __construct<br/>"; }
Public Function __destruct () {//destructor } Final public Function fun () { /* Final keyword (appearing in PHP5) You want the method to not be overwritten by other classes, you can use the final
When this keyword is used to modify a class, then the class will not be inherited by other classes (can be instantiated)
Note: This keyword cannot be used to modify variables */ } }//end Class /******************************************* Class object Instance description (abstract class) *******************************************/ Abstract class abstractdemo{ /***************************************
1. Abstract classes cannot be instantiated
2. Abstract classes do not necessarily include an abstract method. In other words, abstract classes can have no abstract method
3. Once the abstract method is included, the class must be Declaration as abstract
4. Abstract classes cannot have function bodies
5. If a class continues an abstract class, then he must implement the All abstract methods of the abstract class. (unless it itself is also declared as an abstract class)
***************************************/ } /******************************************* Class object Instance description (inheriting Class) *******************************************/ Class Demotwo extends classdemo{
} /******************************************* Class Object Instance Description (interface) *******************************************/ Interface face{ /********************************* 1. When a class implements an interface, it requires that the class must implement all the methods of this interface 2. Interface methods can not have the method body 3. Cannot instantiate an interface 4. An interface can have properties, but it must be constant, and it is public When to use the interface
1. Fixed specification 2. Set specifications for other programmers to implement, such as: **********************************/ Public function Name ();
}
Interface Face2 extends face{ /*******************************
Inheriting interfaces Ways to implement the parent interface when inheriting interfaces
********************************/
Const NAMEVAR=20;
}
Class Demo implements face2{ /**********************************
Implements interfaces that enable multiple interfaces to be implemented simultaneously When a class implements some interfaces, it must implement all of the interface's methods.
**********************************/
Public $Name 1=0;
Public Function Name () {
Echo Face2::namevar;
}
} ?> |