PHP object-oriented implementation code_PHP tutorial
Jul 21, 2016 pm 03:42 PM
final
php
code
Scope
create
copy
accomplish
object
Will
of
Simple
kind
rewrite
Prevent
For
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","<br>";
}
}
$classA = new ClassA();
$classA->setName( "NameA");
$classA->setTitle("TitleA");
$classB = clone $classA;
$classB->setName("NameB");
echo $ classA->getName(),"<br>",$classA->getTitle(),"<br>";
echo $classB->getName(),"<br>" ,$classB->getTitle(),"<br>";
/* 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.
Statement of this Website
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

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
