php类知识点滴---魔术方法,系统在特定时机触发的方法
- __get()获取私有或受保护属性时调用的方法
<?php
class coach
{
private $chairfit = "徐晓冬";
public function __construct()
{
echo "欢迎来到~必图拳馆训练~";
}
public function __get($chairfit)
{
echo $chairfit;//只打印属性名
}
}
class xxd extends coach
{
public function __construct()
{
echo "像个男人一样去战斗!"."\n";
} }
$cj = new xxd();
$cj ->chairfit;
?>
输出结果:
像个男人一样去战斗! #构造方法调用结果
chairfit #继承自父类的__get()方法
#这样可以访问父类私有属性的值
<?php
class coach
{
private $chairfit = "徐晓冬";
public function __construct()
{
echo "欢迎来到~必图拳馆训练~";
}
public function __get($chairfit)
{
if ($chairfit=='chairfit')
{
return $this->chairfit;
}
}
}
class xxd extends coach
{
public function __construct()
{
echo "像个男人一样去战斗!"."\n";
} }
$cj = new cj();
print($cj ->chairfit);
?>
输出结果:
像个男人一样去战斗! #构造方法调用结果
徐晓冬 #通过父类的__get()方法访问父类私有属性
- __set()对私有或受保护属性设置值
<?php
class coach
{
protected $chairfit = "徐晓冬";
public function __construct()
{
echo "欢迎来到~必图拳馆训练~";
}
public function __get($chairfit)
{
if ($chairfit=='chairfit')
{
return $this->chairfit;
}
}
}
class cj extends coach
{
public function __construct()
{
echo "欢迎来到北武堂训练"."\n";
}
public function __set($name,$value)
#当然,这__set()方法也可以写到父类中去,这里仅仅是为了展示继承特性中子类可以实现对父类属性,方法的继承
{
echo "hello,我是".$value.",你的巴西柔术教练!";
} }
$cj = new cj();
$cj ->chairfit='劲儿弟弟';
?> 输出结果:
欢迎来到北武堂训练
hello,我是劲儿弟弟,你的巴西柔术教练!
- 拓展:在__set()或者__get()中,如果涉及多个不能直接访问的属性(例如:受保护或私有属性)
<?php
class coach
{
protected $chairfit = "徐晓冬";
private $lover = "丁大锅";
public function __construct()
{
echo "欢迎来到~必图拳馆训练~";
}
public function __get($chairfit)
{
return $this->$chairfit;
#注意;这里$chair是形参,如果写作chairfit,那么无论在实例化对象中访问设置什么属性,只会访问或修改父类中的属性protected $chairfit
} }
class cj extends coach
{
public function __construct()
{
echo "欢迎来到北武堂训练"."\n";
}
public function __set($name,$value)
{
$this->$name = $value;
#注意;这里$name是形参,如果写作name,那么无论在实例化对象中访问设置什么属性,只会访问或修改父类中的属性protected $chairfit
}
public function whoilove($lover)
{
echo "我喜欢".$lover;
}
}
$cj = new cj();
print("掌门人是:".$cj->chairfit."\n");
print("我喜欢:".$cj->lover."\n");
?>
输出结果:
欢迎来到北武堂训练
掌门人是:徐晓冬
我喜欢:丁大锅
根据上面的代码,继续拓展
$cj->whoilove($cj->lover);
$cj->lover='陈培昌'; #调用了__set()方法实现对父类私有属性$lover的设置
$cj->whoilove($cj->lover)
输出结果:
我喜欢丁大锅
我喜欢陈培昌
php类知识点滴---魔术方法,系统在特定时机触发的方法的更多相关文章
- php类知识点滴---类的实例化,构造函数,继承初步
实例化类----黑科技用法,通过字符串来实例化 class coach { public function __construct() { echo "欢迎光临北武堂"." ...
- php类知识点滴---类继承的一些原则
完全重写 <?php class coach { public function __construct() { echo "欢迎来到~北武堂训练~"; } } cl ...
- jQuery绑定以及解除时间方法总结,以及事件触发的方法
一 off()和on() $("obj").on(event,[sesect],[data],fn);一般情况下参数只有两个,事件以及事件的处理函数 ...
- php类知识---魔术方法__toString,__call,__debugInfo
<?php class mycoach{ public function __construct($name,$age) { $this->name = $name; $this-> ...
- Python 基础之面向对象之类中的方法和与类相关的魔术属性以及反射
一.类中的方法 普通方法: 没有参数,只能类调用绑定方法: (1)绑定到对象(自动传递对象参数) (2)绑定到类(自动传递参数)静态方法: 无论是类还是对象,都能调用的方法#例:class Dog() ...
- PHP 类中的魔术方法
定义: PHP类中以两个下画线“__”开头的方法被称为魔术方法. 分类: 例如:构造方法:__construct:析构方法:__destruct:动态重载:__set().__get().__call ...
- python类:magic魔术方法
http://blog.csdn.net/pipisorry/article/details/50708812 魔术方法是面向对象Python语言中的一切.它们是你可以自定义并添加"魔法&q ...
- Python面向对象5:类的常用魔术方法
魔术方法就是不需要人为调用的方法,基本是在特定的时刻自动触发- 魔术方法的统一的特征,方法名被前后各两个下滑线包裹 - 操作类 - `__init__`: 构造函数 - `__new__`: 对象实例 ...
- (转)python类:magic魔术方法
原文:https://blog.csdn.net/pipisorry/article/details/50708812 版权声明:本文为博主皮皮http://blog.csdn.net/pipisor ...
随机推荐
- Stream系列(八)Reduce方法使用
裁减计算 视频讲解: https://www.bilibili.com/video/av77715582/ EmployeeTest.java package com.example.demo; i ...
- 【转帖】赤壁之战,曹操大败只因缺了Service Mesh
赤壁之战,曹操大败只因缺了Service Mesh 本文作者把微服务向 Service Mesh 的进化融入到了三国故事中,妙趣横生.故事比较长,大家慢慢看,精彩的在后边. http://develo ...
- IE浏览器中判断IE版本
<!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--><!--[if IE]> 所有的IE可识别 <![e ...
- winform中使用TextBox滚动显示日志信息
代码如下: private void ShowInfo(string msg) { this.BeginInvoke((Action)(() => { textBox1.AppendText(s ...
- T100弹出是否确认窗体方式
例如: IF NOT cl_ask_confirm('aim-00108') THEN CALL s_transaction_end(') CALL cl_err_collect_show() RET ...
- WebMvcConfigurationSupport跨域和fastjson全局替换
@Configuration public class WarnWebMvcConfigurationSupport extends WebMvcConfigurationSupport { /** ...
- url协议+域名+端口号
string url = System.Web.HttpContext.Current.Request.Url.Scheme + "://" + ...
- JS ES5
常用 严格模式 use strict 必须使用var声明变量 禁止自定义函数this指向window 'use strict' funcion Person(name){ this.name = na ...
- Joomla 3.0.0 - 3.4.6 RCE漏洞分析记录
0x00 前言 今天早上看到了国内几家安全媒体发了Joomla RCE漏洞的预警,漏洞利用的EXP也在Github公开了.我大致看了一眼描述,觉得是个挺有意思的漏洞,因此有了这篇分析的文章,其实这个 ...
- 第十一章、特性property
目录 第十一章.特性property 一.property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 二.为什么要用property 三.封装与拓展性 第十一章.特性property ...