Class: Abstracted by many objects.
Object: instantiated through the class
To define a class:
Class Info
{
Const P = 3.1425926; Defines what is commonly used in certain classes as constants for ease of use
public static $class; The static keyword defines the statically
Public $code;
protected $name;
Private $sex;
The static function Test ()//static method
{
echo self:: $class; Call a static member inside a class
echo "static Method! ";
}
function __construct ($s)
{
$this->sex = $s;
}
function Say ()
{
echo self:: $class; Static members can be called in normal methods
echo "Hello";
}
function Getsex ()
{
return $this->sex;
}
function Setsex ($s)
{
$this->sex = $s;
}
function __set ($n, $v)
{
$this $n = $v;
}
function __get ($n)
{
return $this $n;
}
}
Instantiation of objects
$r = new Info ("female");
$r->code = "Join";
$r->sex;
/*info:: $class = "0305";
$r->say (); */
/*info:: $class = "0305"; accessing static members
Info::test (); */
Three major features
Packaging:
Purpose: To make the class more secure, avoid direct access to member variables by the outside world
Procedure: 1. Change member variables to private
2. Indirect manipulation of member variables through methods (functions)
Inherited
Concept: If a class has subclasses, that subclass inherits everything from the parent class (the private member cannot access it).
You need to add a keyword when defining a subclass: Extends
Features: Single inheritance, a son can only have a father, a class can have only one parent class
If there is a constructor in the parent class, the subclass is instantiated according to the criteria of the parent class constructor.
Subclasses override of Parent class methods: Write the same method in a subclass
Final: Used to decorate a class, which means that the class is a final class that cannot be inherited
/*class Test extends Info
{
Public $birthday;
function Run ()
{
echo "Run";
}
function Say ()
{
Parent::say (); Call the Say () method of the parent class
echo "Test Say";
}
}
$t = new Test ("female");
$t->code = "P001";
Echo $t->sex;
$t->say ();
$t->run (); */
Static members
The ordinary members of a class belong to the object, not to the class (called when the object is called)
What is static: A class static member belongs to a class, not to each object.
Defining static members with the Static keyword decoration
Static methods cannot invoke ordinary members
A static method can call a static member and use the Self keyword to invoke
Self represents the class, $this represents the object
PHP Object-oriented inheritance