/*
Class Ren
{
public static $color;//static
static function ()
{
ren:: $color;
Self:: $color;//self can only be written in the class, representing this category
}
}
*/
Classes that cannot be instantiated: abstract classes
Abstract class animal//keyword abstract
{
Public $dong;
Public $jiao;
function Chi ()
{
}
function Shui ()
{
}
}
Class Ren extends Animal
{
}
$d =new Ren ();
Var_dump ($d);
Interfaces: Extremely abstract classes, in which the methods are rewritten
Keyword interface
The inside method has no function body
The keyword used to implement the interface is implements, not extends
Subclasses that implement an interface must implement each method in the interface
Interface Animal2//Don't add Class
{
}
Class Ren2 extends Animal2
{
}
Instance
Interface USB
{
function Read ();
function Write ();
}
Class Mouse implements USB
{
function Read ()
{
echo "Inserted mouse";
}
function Write ()
{
echo "Power to Mouse";
}
}
$m =new Mouse ();
$m->read ();
$m->write ();
destructor: Frees the memory before the object is destroyed, links close and so on
Class Ren
{
Public $name;
destructor method
function __destruct ()
{
echo "The object was destroyed";
}
}
Special wording: __destruct
Special execution time, execution when destroyed
$r =new Ren ();
$r->name= "Zhang Dan";
Var_dump ($R);
ToString can be called when the object is output and must have a return value
Class Ren
{
Public $name;
Public $sex;
Public $age;
function Run ()
{
}
Function Show ()
{
echo "name represents name, sex representative, age";
}
function __tostring ()
{
Return "name represents name, sex representative, age";
return $this->name;
}
}
$r =new Ren ();
$r->show ();
Echo $r;
Other small knowledge points
$a = 10;
$b = 20;
$c = 25;
if (!isset ($b))//Determine if there is
{
echo "Variable B does not exist";
exit;//Exit Program
Die ("Variable B does not exist!") ");//Output error message and exit the program
}
echo $a + $b;
Echo $a * $b;
Output array
$attr =array (1,2,3,4);
Var_dump ($ATTR);
Print_r ($ATTR);
echo "Hello", "haha";//can output multiple strings
print "helo";//Output only one
Load class naming: ren.class.php
Class Ren
{
Public $name;
Public $sex;
Public function Say ()
{
echo "Hello";
}
}
Referencing classes, loading classes
Include ("ren.class.php");
Require_once ("ren.class.php");//write at the top of the file, if an error occurs, the code stops executing
Require_once "ren.class.php";
Auto Add-ons
1. Naming all classes of files, requiring the use of the same rule
2. There must be a class name inside the file name
3. All class files must be in the same folder
With very little,
function __autolode ($classname)
{
Require $classname. " Class.php ";
}
0607AM Abstract Classes & Interfaces & refactoring Methods &tostring& Small knowledge points