Home  >  Article  >  Backend Development  >  PHP object-oriented implementation code_PHP tutorial

PHP object-oriented implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:42:54770browse

1. Simple object creation

Copy code The code is as follows:

//The scope of the class final: will prevent subclasses Rewrite this field
//The scope of the method abstract: declare in the parent class, implement in the subclass
//Define the class:
class Employee{
//Define the field
private $name;
protected $title;
public $wage;
//Constant
const PI = 3.1415926;
//Static member variable
private static $visitors = 0;
//Define the constructor
function __construct(){
//Use static member variables
self::$visitors++;
echo "constructor";
}
/ /Define destructor
function __destruct(){
echo "destruct";
}
//Declaration method
public function clockIn(){
//Use fields
echo "Member $this->name";
}
//When accessing attributes that an object does not own (such as private fields), if the object uses __get and __set methods, it will Automatically call the __get method or __set method
function __set($property,$value){
$this->$property = $value;
}
function __get($property){
return $this->$property;
}
}
//Class inheritance Manager inherits Employee
class Manager extends Employee {
function __construct(){
//Call the constructor or method of the parent class
parent::__construct();
parent::clockIn();
echo "Manager constructor";
}
}
//Create object
$employee = new Employee();
$employee->wage = 10000;
//Use constant
echo Employee::PI;
//Call method
$employee->clockIn();
$manager = new Employee();
//instanceof determines whether an object is an instance of a class, a subclass of a class, or implements a specific interface
if($manager instanceof Employee ) echo "Yes";

2, Advanced OO features
(1) Object cloning
Copy code The code is as follows:

//Object clone
class ClassA{
private $name;
private $title;
public function setName($ name){
$this->name = $name;
}
function getName(){
return $this->name;
}
public function setTitle( $title){
$this->title = $title;
}
public function getTitle(){
return $this->title;
}
function __clone (){
echo "I was cloned","
";
}
}
$classA = new ClassA();
$classA->setName( "NameA");
$classA->setTitle("TitleA");
$classB = clone $classA;
$classB->setName("NameB");
echo $ classA->getName(),"
",$classA->getTitle(),"
";
echo $classB->getName(),"
" ,$classB->getTitle(),"
";
/* output
I was cloned
NameA
TitleA
NameB
TitleA
* /

(2) Interface
Copy code The code is as follows:

//Interface
interface IPillage{
function method();
}
class ClassC extends ClassA implements IPillage {
function method(){
echo "inteface method";
}
}
$classC = new ClassC();
$classC->method();
//inteface method

(3) Abstract class
Copy code The code is as follows:

//Abstract class is a class that cannot be instantiated and can only be used as a base class for other classes to inherit from
abstract class BaseClass{
protected $name;
abstract function method();
}
class ChileClass extends BaseClass {
function method(){
echo "method";
}
}
$child = new ChileClass();
$child->method();
//output method

Note:
If you want to create a model that will be adopted by some closely related objects, you can use abstract classes. Use interfaces if you want to create functionality that is adopted by a number of unrelated objects.
If behavior must be inherited from multiple sources, use interfaces. PHP can inherit multiple interfaces, but cannot extend multiple abstract classes.
If you know that all classes will share a common behavior implementation, use an abstract class and implement that behavior within it. Behavior cannot be implemented in interfaces.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320862.htmlTechArticle1. The simple object creation copy code is as follows: //The scope of the class final: will prevent subclasses from re-creating Write this field //The scope of the method abstract: declared in the parent class, implemented in the subclass/...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn