PHP can also implement object-oriented programming in addition to being simple to implement specific features, and it can be elegant.
Use the specific code examples below to describe concepts related to object-oriented inheritance.
<?php/** * Author:helen * CREATETIME:2016/4/10 9:58 * Description: Object-oriented-inheritance (rewrite (polymorphism), encapsulation, Visibility)/namespace Home\co
Ntroller\program;
Use Home\controller\commoncontroller; Class Inheritcontroller extends commoncontroller{//object-oriented programming test method public Function main () {/* * Insta
Nceof keywords can be used to see if a particular object is a type that is not part of a particular class * */$dog = new Dog (' Helen ');
$cat = new Cat (' Huihui ');
$dog->eat ();
$cat->eat ();
$dog->sleep ();
$cat->sleep ();
$dog->fetch ();
$cat->climb ();
$dog->play ();
$cat->play ();
Unset ($dog, $cat);
Echo ' <br> ';
$side = 40;
$square = new Square ($side);
echo $square->getarea ();
Unset ($square);
Echo ' <br> ';
$parent = new Test ();
$child = new Littletest ();
$parent->printvar (' public ');
$child->printvar (' public '); $parent->public = ' Modified ';
$parent->printvar (' public ');
$child->printvar (' public ');
$parent->printvar (' protected ');
$child->printvar (' protected ');
$parent->printvar (' private ');
$child->printvar (' private ');
$this->test ();
$this->test ();
$this->test ();
$obj = new SomeClass (); echo $obj instanceof SomeClass;
Output 1 if ($obj instanceof SomeClass) {echo ' true ';
}else{echo ' false ';
echo $obj:: $counter;
$obj 1 = new SomeClass ();
echo $obj 1:: $counter;
The function test () {static $n = 0;
echo "$n <br>";
$n + +; }//Parent class class parentclass{}//Inheritance class/* Inheritance constructors and destructors * in PHP, when you create an object of a subclass, the constructor of the parent class is not automatically invoked * * * * * * * * * * * * */* * Rewrite (created polymorphic) * Subclass The method defined must have exactly the same name and number of parameters as the method of the parent class (a technique with a different number of parameters is called the "overload" of the method) * The method defined as final cannot be overridden by any subclass */* * Access Control (encapsulation) * 1, the visibility of the property must be specified in PHP (default public
)。 * 2, public, can be accessed from any place: inside the class itself, derived subclasses, and itsHis class.
* 3, Protected, can only be accessed in the class itself and in subclasses.
* 4, private, can only be accessed in the class itself (the private variable name usually begins with an underscore) * effectively avoiding bugs and unnecessary behavior (for example, ability to change the attributes of a class * *//* Range Resolution operator (::) * 1, for accessing members in a class (not an object)
* 2, when using the class, when the parent class and subclasses have the same properties and methods, use it to avoid confusion.
* 3, outside the class, use the operator to access the members of the class without creating an object.
* 4, use the class name outside the class, and in the class, use the Self keyword.
* 5, to refer to a member of the parent class, you can use the parent keyword and the scope resolution operator to refer to it.
* *//* Static member * 1, the static property in the class is equivalent to the static variables in the function.
* 2, a static function variable can remember its value every time the function is called.
* 3, the concept of static members of the class is the same, add the static keyword.
* 4, static properties, can not use $this access in the class, you should use self followed by the range resolution operator (::), followed by the variable name with the dollar sign.
* *///* Class constant * 1, class constant and static property is the same, it can be the class (or its subclasses) all instances of access.
* 2, the value can not be changed.
* 3, the method of creating a class constant is to use the Const keyword followed by the constant name (no dollar sign). * 4, cannot access through the object, should use $obj::P i * 5, can be used anywhere classname::constant_name, class using Self::constant_name * * */class ChildClass
Extends parentclass{}//demo sample class pet{public $name;
function __construct ($pet _name) {$this->name = $pet _name;
Self::sleep ();
Function eat () {echo "<p> $this->name is eating.</p>"; function sleep () {echo <p> $thiS->name is sleeping.</p> ";
function Play () {echo ' <p> $this->name is playing.</p> ';
The class Cat extends pet{function climb () {echo "<p> $this->name is climbing.</p>";
function Play () {parent::p lay ();
echo "<p> $this->name is climbing.</p>";
The class Dog extends pet{function fetch () {echo "<p> $this->name is fetching.</p>";
function Play () {parent::p lay ();
echo "<p> $this->name is fetching.</p>";
}//rectangle Classes Class rectangle{public $width = 0;
Public $height = 0;
function __construct ($width =0, $height =0) {$this->width= $width;
$this->height= $height;
function SetSize ($width, $height) {$this->width= $width;
$this->height= $height;
function Getarea () {return ($this->width* $this->height); } function Getperimeter() {return ($this->width+ $this->height) *2);
The function Issquare () {if ($this->width== $this->height) {return true;
}else{return false;
Class Square extends rectangle{function __construct ($side =0) {$this->width = $side;
$this->height = $side;
Class test{Public $public = ' public ';
Protected $protected = ' protected ';
Private $private = ' private '; function Printvar ($var) {echo <p>in test,\$ $var:. '
{$this-> $var} ' .</p> '; The class Littletest extends test{function Printvar ($var) {echo <p>in littletest,\$ $var:. '
{$this-> $var} ' .</p> ';
Class someclass{public static $counter = 0;
function __construct () {self:: $counter + +;
}
}
Programming is a technology, but also an art.