Home>Article>Backend Development> What is object-oriented PHP? PHP object-oriented summary
The content this article brings to you is what is object-oriented in PHP? PHP object-oriented summary. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Object-oriented features:
*Reusability (each module can be reused in the project)
*Flexibility (each module can be easily replaced and changed)
*Extensibility (it is very convenient to add new functions to the module)
The relationship between classes and objects(Classes generate objects )
Birds of a feather flock together: Objects with the same characteristics are grouped into one category. For example, NBA players have attributes that come to mind such as name, height and weight, team number, and methods such as running, jumping, dribbling, and shooting. Treat them as a class, and Jordan and James are instances of NBA players, also called objects;
The object has high internal cohesion (specific functions, all related content is encapsulated inside the object), and external low cohesion Coupling (some visible attributes and some visible methods of the object)
Use class to declare a class. There can only be member attributes and member methods in the class! ! ! ! There cannot be others. There cannot be two classes with the same name in one script. The instantiated class can have multiple objects
class Computer{ public $text = "用关键字new实例化一个类"; public function output(){ echo $this->text; } } $computer = new Computer(); $computer->output();//外部访问方法 $computer->text();//外部访问属性 $computer2 = new Computer();//可以实例化多个
Attributes and methods of the class
Access control:
public Public can access
private Private can only be accessed within the class itself
protected Subclasses of protected inheritance and the class itself can be accessed internally, but cannot be accessed externally, such as: $computer->output() ;If the output() method is set to protected, an error will be reported.
Methods in the class:
When calling an attribute in the method, you need to instantiate the class to access the attribute, and $this can be understood as an instance of this class
class Computer{ public $text = "用$this调用我"; public function output(){ echo $this->text; } }
Constructor method(executed when instantiating)
The constructor can pass parameters, which are passed in during instantiation
Passed through the parameters of the constructor , change the properties inside to make each instance object different
class Computer{ function __construct($arg){ echo "我是构造方法,自动执行"; echo $arg; } } $conputer = new Computer("我是构方法的参数");
Destruction method:Executed when destroyed, no parameters allowed
Automatically executed after the program ends, manual release ( When all references to the object are destroyed), the destructor will be executed in advance, and there is no need to wait until the end of the program to call it.
When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the variable content is destroyed
//对象引用 在PHP 中引用的意思是:不同的名字访问同一个变量内容. class A { function __destruct() { echo '====我是析构函数===='; } } $a = new A(); //提前执行析构函数的两个方法 unset($a);$a=null; echo "====正常执行我是先输出的====="; //两种对象引种的区别 $a = $b;//这时$a和$b是两个独立的引用,unset($aa)只是删除了$a的引用,还有$b的引用,所以不会执行析构函数。改变其中一个为null,不会影响另外一个变量,也不会执行析构函数; $c = &$a;//两个的变量使用同个引用,c是a的影子,改变其中一个变量为null,两个都为null,会执行析构函数。但是只是unset($a),$c还在使用引用,只要有引用在不会执行析构函数; //对象的引用用于访问对象的属性和方法,所以$a $b $c都可以访问对象的属性方法; //当一个对象的所有引用被删除后,才会触发析构函数 //总结:如果程序大,对象引用尽量使用&,节省内存,清除也方便。设其中一个为Null,其他都为Null; //变量引用 (默认变量赋值会独立开辟一个内存存放内容) //PHP 的引用允许你用两个变量来指向同一个内容 $a="AB"; $b =&$a; echo $a;//这里输出:AB echo $b;//这里输出:AB $b="EF"; echo $a;//这里$a的值变为EF 所以输出EF echo $b;//这里输出EF //函数引用 "; echo $b;//输出101 ?>//函数的引用返回
The difference between assignment and cloning:
$a=$b (independent reference) and $a=&$ The assignment method b (same reference) points to the same object and the same memory space; the assignment method
$a=clone $b points to another new object, another block of new memory space.
name = "Anne"; echo $a->name,"\n",$b->name,"\n",$c->name,"\n",$d->name;//输出为:Anne Anne Anne Peter ?>
Class constant
Internal access self::constant name
External access does not require instantiation: class name::constant name
class Computer{ const yh = 12; const yh2 = self::yh+1; } var_dump (Computer::yh2);
The difference between $this and self
One is that the object $this is equivalent to instantiation. It can be said that except for static and const constants, basically everything else can be contacted using this;
self is the class itself. Self can access the static properties and static methods in this class, and can access the static properties and static methods in the parent class. When using self, you don’t need to inherit the instantiated
class
The meaning of existence: extract the same attribute methods and let the subclass inherit
## There can only be one comma after #extends, which means that only one parent class can be inherited. Single inheritanceclass dad{ function eat(){ echo "我是父类的方法,子类可以继承我的方法"; } } class boy extends dad{ } $boy = new boy();$boy->eat();The subclass can also override the method of the parent class When the subclass has no constructor method, it will be called If you have a constructor method of the parent class, you can call your own.
Use the keyword parent:: __construct(); to call the constructor method of the parent class.
If you add the final keyword in front of the class, it will not be inherited; add In front of the method, this method will not be overridden
Namespace(solve the problem of naming conflicts)Using the principle that the file system does not use directories
Introduced php files Add namespace directory\subdirectory;
When used, call: new directory\subdirectory\class name(); such as new com\session\computer();
Affected by the namespace are classes (including abstract Classes, traits), interfaces, functions, constants (const and define are both used to declare constants (their differences are not detailed), but in the namespace, define acts globally, while const acts on the current space .)
//例一 //在脚本前面写了一些逻辑代码 //例二 //在脚本前面输出了一些字符 为什么要说第一个命名空间呢?因为同一脚本文件中可以创建多个命名空间。Since each call must be written It is very troublesome to go up the path, so the usage of use appears (use must be introduced with a class name or function name)
When calling, use use to introduce first, use directory\subdirectory\class; you can also use aliases to distinguish them as
use venter\session\computer; use venter\computer as computer2; new computer(); new computer2(); 函数的话记得带上function use function venter\session\computer; computer(); 常量记得带上const use const venter\session\computer; var_dump(computer);Key points: The difference between calling global classes, functions, and constants in the namespace.
Page a defines the namespace namespace venter; after introducing page b (global space), you want to call page b at this time Class, must:
New \venter() Add a \ to represent
in the global space. But when calling functions and constants, there is no need to add it, because it will automatically search and search globally.
类自动加载:
当你实例化一个类时,这个类不存在,就会自动执行我们写的 function __autoload($className){},在这个自动函数的内部去执行我们想要操作的代码,参数就是我们实例化的类名,因此可以根据参数去引入php文件。
__autoload将被淘汰,使用新的函数 spl_autoload_register();参数可以传函数名或者匿名函数
spl_autoload_register(function($a){ echo $a; } ); new Computer(); 或者 function aaa($a){ echo $a; } spl_autoload_register('aaa'); new Computer(); 或者类的调用方法 class momo { function aaa($a){ echo $a; } } spl_autoload_register([new momo],'aaa'); new Computer();
利用命名空间和自动加载实现项目自动引用
1,文件目录就和命名空间名一样,文件名和类名一样 new models\imooc() 就是在models文件下的imooc.php文件,
2,然后利用spl_autoload_register(function($classname){
include str_replace("\\","/",$classname.".php");
});
这里的作用是models\imooc替换掉models/imooc.php 引入
而imooc.php文件中必须命名 namespace models;
static 静态
存在的价值,为了让对象公用一些数据,做到统一性
当属性和方法被定义为 static 时就变成类的属性和方法,不属于对象的,不需要实例化
外部访问 类名::$aa 内部访问 self::$aa 或者static::$aa; 子类访问父类 parent::$aa;
静态方法不能调用非静态属性
因为 $this代表实例化对象,而这里是类,不知道 $this 代表哪个对象
class person{ public static $aa ="a"; public static function eat(){ echo self::$aa; echo static::$aa; } } class boy extends person{ public function eat2(){ echo parent::$aa; } } echo(boy::eat2());
后期静态绑定:
class A{ public static function who(){ echo "A类的who方法"; } public static function test(){ self::who(); // static::who(); } } class B extends A{ public static function who(){ echo "B类的who方法"; } } B::test(); 如果是self::who(); 输出的是A类的who方法,说明调用的是A自身,static::who();会根据上下文作处理,类B调用就使用B里面的who()
魔术方法
__set($var,$val) 设置私有属性,当外部调用设置一个私有属性时会自动执行这个方法,
__get($var)获取私有属性,原理是外部调用一个私有属性时会自动执行这个方法
例子:
class test{ private $abc = ""; private $ccc = ""; public function __set($var,$val){ $this->$var = $val; } public function __get($var){ return $this->$var; } } $test = new test(); $test->abc = 'abc'; var_dump($test->abc);
__isset($var)方法,当外部调用isset($test-abc)访问一个不能访问的属性时,就会自动执行 isset()或者empty() 都会触发__isset()方法
__unset($var),当外部想删除一个私用属性时,会自动执行
以上是属性的重载
__call,__callStatic是属于方法的重载,不是重写
__call($func方法名,$arguments参数)当外部调用一个没有定义的方法时,就会调用
__callStatic($meethod,$arg)当调用一个不存在的静态方法时,会自动执行 注意在function 前也要加入 static 因为是静态方法
__invoke($rag)当实例化一个类后,把这个对象变量当成函数调用时,会自动执行
例如:$test = new test();
$test("go....");
__toString() 当对象被当成string使用时,会执行,比如 echo($test);
__clone方法的一些应用
当一个对象被克隆时,会调用__clone,此时可以在里面执行一些逻辑,比如不想让某些值被克隆,或者初始化一些属性的值
对象拷贝:
浅拷贝:是地址传递(对象是浅拷贝)
深拷贝:复制一份,新开辟一块内存(普通变量是深拷贝)
加clone 使对象成为深拷贝,互不影响;
$a = new A();
$b = clone $a;
__clone()当使用clone时,会自动执行
$a = new A();
$b = clone $a; 在A类中会执行__clone()
例子:
class A{ public $obj = null; public function __clone(){ $this->obj = clone $this->obj;//开辟了新空间给克隆的对象 } } Class B{ public $sex = 0; } $a = new A(); $a->obj= new B(); $b = clone $a; $b->obj->sex = 1; var_dump($a->obj->sex);
类型约束
class A{ public $obj = null; public function go(){ echo "googogogogo"; } } class B{ public function go(){ echo "bbbbbb"; } } Function test(A $a){ $a->go(); } test(new A());//必须是A的实例
trait 关键字
解决单继承问题 配合use 使用
trait A{ public function a(){ echo "aaaaa"; } } trait B{ public function b(){ echo "bbbb"; } } Class test{ use A,B; public function c(){ echo "cccc"; } } $test = new test(); $test->a(); $test->b(); $test->c();
接口
可以理解为类的模板 不能直接被实例化
用instanceof 判断某个对象是否实现了某个接口
接口可以继承接口,但是类必须实现接口所有的方法
interface person { public function eat(); public function sleep(); } class man implements person{ public function eat(){ echo "chichi吃吃"; } public function sleep(){ echo "shuishushi"; } } $aaa = new man(); $aaa->eat(); //接口继承 两种方法 interface aa { public function eat(); } interface bb { public function sleep(); } interface person extends aa,bb{ //person就直接继承了两种方法 } class man implements person{ public function eat(){ echo "chichi"; } public function sleep(){ echo "shuishuishui"; } } //或者 直接实现两种接口 class man implements aa,bb{ public function eat(){ echo "chichi"; } public function sleep(){ echo "shuishuishui"; } } //接口里面可以利用 const 定义一个常量 interface person { const aa = "bbb"; public function eat(); public function sleep(); } echo person::aa;
抽象类:
(比如人和动物都会呼吸方法都一样,此时就不需要每个类去实现不同的呼吸方法,吃东西的方法不同,所以在类中各自实现逻辑)抽象类的子类只需要实现抽象方法;
内部有自己的执行方法
abstract class person { public function holiday(){ echo '我是抽象类自己实现的方法'; } //抽象方法,以分号结束,无方法体;抽象类中至少要有一个抽象方法 abstract public function eat(); } class man extends person{ public function eat(){ $this->holiday();//直接调用即可 } } $aaa = new man(); $aaa->eat();
单例模式:
让一个类只会被实例化一次,节省内存空间
先把构造函数和克隆函数变成私用属性
class test{ private static $_instance = null;//依靠属性存储实例对象判断是否已经实例过 private function __construct(){ // 私有后将不会被外部实例 } private function __clone(){ // 私有后将不会被外部克隆 } public static function getInstance(){ if(!(self::$_instance instanceof self)){ // instanceof判断是否是当前的实例,不是的话就实例化赋值,否则就返回 self::$_instance = new self(); } return self::$_instance; } } $test1 = test::getInstance(); $test2 = test::getInstance(); $test3 = test::getInstance(); var_dump($test1); var_dump($test2); var_dump($test3); // 输出结果object(test)#1 (0) { } object(test)#1 (0) { } object(test)#1 (0) { }
工厂模式:
就是在多处实例化一个类,当这个类名需要改变时,就会导致大量地方需要更改,使用工厂模式避免这种情况发生
class A{ public function a(){ echo 'aaa'; } public function c(){ echo 'ccc'; } public function b(){ echo 'bbb'; } } class cashe{ public static function factory(){ return new A();//当业务逻辑变化时,只需要改此处 } } $cashe = cashe::factory(); $cashe->a();
面向对象功能汇总
有继承功能,必须有访问的控制,(私有公有属性),static静态关键字来保存一些公用的数据,重写对父类的方法属性重定义,final关键字不允许重写或者继承,访问静态属性的几个方法,接口中的方法是不用具体实现的,在类中才实现,而且必须实现接口中所有的方法,因为有了不同的实现方法逻辑,不同的对象产生不同的表现,这叫多态,存在与接口与类之间的叫抽象类,可以有一部分可以实现,一部分不用实现
/*--------------多态的一个应用实例 模拟USB设备的使用------------------*/
//一个USB的接口 interface USB{ function mount();//装载USB的方法 function work();//USB工作的方法 function unmount();//卸载USB的方法 } //定义一个USB设备 U盘 class Upan implements USB{//实现USB接口 function mount(){ echo " U盘 装载成功
"; } function work(){ echo "U盘 开始工作
"; } function unmount(){ echo "U盘 卸载成功
"; } } //定义一个USB设备 USB鼠标 class Umouse implements USB{//实现USB接口 function mount(){ echo " USB键盘 装载成功
"; } function work(){ echo "USB键盘 开始工作
"; } function unmount(){ echo "USB键盘 卸载成功
"; } } //定义一个电脑类 class Computer{ //使用USB设备的方法 function useUSB ($usb){//$usb参数表示 使用哪种USB设备 $usb->mount();//调用设备的 装载方法 $usb->work();//调用设备的 工作方法 $usb->unmount();//调用设备的卸载方法 } } //定义一个电脑的使用者的类 class PcUser{ // 安装 USB的方法 function install(){ //首先拿来一台电脑 $pc=new Computer; //拿来一些USB设备 $up=new Upan;//拿来一个U盘 $um=new Umouse;//拿来一个USB鼠标 //把USB设备插入电脑, 使用电脑中使用USB设备的方法 来调用 要插入的设备 $pc->useUSB($up);//插入U盘 $pc->useUSB($um);//插入USB鼠标 } } //实例化一个电脑用户 $user=new PcUser; $user->install();//安装设备 /*-------------输出内容-------------- U盘 装载成功 U盘 开始工作 U盘 卸载成功 USB键盘 装载成功 USB键盘 开始工作 USB键盘 卸载成功 -----------------------------------*/ ?>
以上就是本篇的全部内容,更多相关教程请访问php编程从入门到精通全套视频教程,php实战视频教程,bootstrap视频教程!
The above is the detailed content of What is object-oriented PHP? PHP object-oriented summary. For more information, please follow other related articles on the PHP Chinese website!