/*********************************************
Class object instance description (common class)
*******************************************/
class ClassDemo{
public $PublicVar;
private $PrivateVar;//Private variables cannot be called externally
protected $ProtectedVar;//Protected variables are not accessible from outside and subclasses
public static $StaticVar=0;//static static variables Static methods cannot access non-static methods and variables, non-static methods and variables can access static methods and variables
const constVar='';//When you have an attribute that you don’t want to modify, consider using const to make it a constant, using class name::constant name; interface name::constant
/*
1. Constants need to be assigned an initial value when they are defined
2. Constants cannot be modified.
3. The constant name cannot contain $, it is usually uppercase, and the default is public
4. Constants are used inside the class self::name of constant classname::name of constant
*/
public function __construct( /*$name*/ ){//This can take parameters. After taking parameters, you must also have parameters when creating, otherwise an error will occur
//$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 ";
}
public function __destruct(){//Destructor
}
final public function fun(){
/*
final keyword (appears in PHP5)
If you want the method not to be overwritten by other classes when inheriting, you can use final
When this keyword is used to modify a class, 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 contain abstract methods.
In other words, an abstract class can have no abstract method
3. Once the abstract method is included, the class must
Declared as abstract
4. Abstract classes cannot have function bodies
5. If a class inherits an abstract class, it must implement
All abstract methods of this abstract class. (Unless it is also declared as an abstract class itself)
****************************************/
}
/*********************************************
Class object instance description (inherited class)
*******************************************/
class DemoTwo extends ClassDemo{
}
/*********************************************
Class object instance description (interface)
*******************************************/
interface Face{
/**********************************
1. When a class implements an interface, the class is required to implement all methods of this interface
2. Interface methods cannot have method bodies
3. Cannot instantiate an interface
4. There can be attributes in the interface, but they must be constants and public
When to use interfaces
1. Set standards
2. Set specifications and let other programmers implement them, such as:
************************************/
public function Name();
}
interface Face2 extends Face{
/*********************************
Inherited interface
When inheriting an interface, you do not need to implement the methods of the parent interface
********************************/
const namevar=20;
}
class Demo implements Face2{
/***********************************
Implementing interfaces can implement multiple interfaces at the same time
When a class implements certain interfaces, it must implement all interface methods
**********************************/
public $Name1=0;
public function Name(){
echo Face2::namevar;
}
}
?>
|