分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

php orient object progarmming

发布时间:2023-09-06 02:26责任编辑:林大明关键词:暂无标签

类:

class ParentClass{ ???????public $a=10; ???????public function setVal($val){ ???????????$this->a = $val; ???????} ??????public function getVal(){ ???????????return $this->a; ?????}}

实例化

$obj = new ParentClass;echo $obj->a."<br>";$obj->setVal(10000);echo $obj->getVal();

构造函数__construct() 当初始化实例的时候会被触发

class obj{ ?public function __construct(){ ?????echo "the class".__class__."was initialized !!!"; ???}}new obj;

__destruct() 当实例对象被注销的时候,会自动触发

class obj{ ?public function __construct(){ ?????echo "the class".__class__."was initialized !!!"; ???} ???public function __destruct(){ ????????echo "Happy Ending !!!"; ???}}$a = new obj;unset($a);

extends 子类继承父类

class childOne extends ParentClass{ ???}new childOne;

当子类存在的变量或则构造函数和父类的相同的时候,会覆盖父类的变量和方法
如果需要保留父类的构造函数同时触发的话,可以使用parent::__construct();

class childOne extends parentClass{ ???public function __construct(){ ???????parent::__construct(); ???????echo "this is my construct!!!"; ???}}new childOne;

访问标识符
public 变量,方法 所有的类都可以访问
protected 变量,方法 该类内部访问或则子类通过方法向上继承的方式才可以访问,子类不可以直接去访问
private 变量,方法 只允许该类自己内部访问

class ParentClass{ ?public $a =2000; ?public function __construct(){ ?????echo "the class".__class__."was initialized !!!"; ???} ???public function __destruct(){ ????????echo "Happy Ending !!!"; ???} ???protected function getVal(){ ????return $this->a; ???}}class childOne extends parentClass{ ???public function __construct(){ ???????parent::__construct(); ???????echo "this is my construct!!!"; ???}}$ch = new childOne;echo $ch->getVal();
class ParentClass{ ?public $a =2000; ?public function __construct(){ ?????echo "the class".__class__."was initialized !!!"; ???} ???public function __destruct(){ ????????echo "Happy Ending !!!"; ???} ???protected function getVal(){ ????return $this->a; ???}}class childOne extends ParentClass{ ????public function abcd(){ ????????return $this->getVal(); ???????????????}}$child123 = new childOne;echo $child123->abcd();

静态变量,静态方法,不需要实例化就可以直接使用的

<?php// Your code here!class demo{ ??public static $count=10; ??public static function plusOne() ?{ ?????return "The count is " . ++self::$count . ".<br />"; ?}}echo demo::$count."<br>"; ?//注意,这里是有$符号的!!!echo demo::plusOne();?>

Reference:
https://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762

php orient object progarmming

原文地址:https://www.cnblogs.com/cyany/p/10123732.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved