PHP Magic Method Setter and Getter
<?php
/*
Magic method __set() and __get()
1.The definition of a magic function is provided by the programmer – meaning you, as the programmer, will actually write
the definition. This is important to remember – PHP does not provide the definitions of the magic functions
– the programmer must actually write the code that defines what the magic function will do. But, magic functions will
never directly be called by the programmer – actually, PHP will call the function ‘behind the scenes’. This is why they
are called ‘magic’ functions – because they are never directly called, and they allow the programmer to do some pretty
powerful things.
2.PHP functions that start with a double underscore – a “__” – are called magic functions (and/or methods) in PHP.
They are functions that are always defined inside classes, and are not stand-alone (outside of classes) functions.
The magic functions available in PHP are: __construct(), __destruct(), __call(), __callStatic(), __get(), __set(),
__isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone(), and __autoload()
3.magic methods can be defined in such a way that they can be called automatically and does not require any function call
to execute the code inside these functions.
4.These special functions should be defined by the user. But no need to call them explicitly. Rather, it will be called
on appropriate event occurrence. For example, class __construct() will be called while instantiating the class.
PHP magic methods must be defined inside the class.
get more info at https://phppot.com/php/php-magic-methods/
*/
//Example address http://www.learningaboutelectronics.com/Articles/Get-and-set-magic-methods-in-PHP.php
class kids
{
private $age;
protected $height =23;
//setter will be invoked when something outside class access inaccessible variables, for encapsulation
//including protected, private and not declared variable,
// but public variable will not invoke the setter and getter, because we can access public variable
public function __set($property, $value)
{
if ($value > 30)
{
$current =$this->$property;
//notice: $this->property(without $) will create a new attribute called property, $property
$this->$property= $value;
echo "You are going to update $property from $current to $value"."<br>";
}else{
echo "Update $property failed!<br>";
}
}
public function __get($property)
{
return "You are trying to get inaccessible $property 's value: " . $this->$property . "<br>";
}
}
$kid= new kids; //() is optional, automatically call __constructor(){}
//1. testing protected variable
$kid->height =55; //this will implicitly and automatically call __set() because height property is protected
//You are going to update height from 23 to 55
$kid->height =23; //Update height failed!
echo $kid->height; //You are trying to get inaccessible height 's value: 55
//2. testing private variable
$kid->age = 37; //You are going to update age from to 37
echo $kid->age; //You are trying to get inaccessible age 's value: 37
//3.testing undeclared variable in the class
$kid->weight =40;
/*You are going to update weight from You are trying to get inaccessible weight 's value:
to 40 */ //why ?
//because $current=$this->$property =$this->weight statement invoke the getter
echo $kid->weight; //40
PHP Magic Method Setter and Getter的更多相关文章
- Python魔术方法-Magic Method
介绍 在Python中,所有以"__"双下划线包起来的方法,都统称为"Magic Method",例如类的初始化方法 __init__ ,Python中所有的魔 ...
- 【原】iOS 同时重写setter和getter时候报错:Use of undeclared identifier '_name';did you mean 'name'
写了那么多的代码了,平时也没有怎么注意会报这个错误,因为平时都很少同时重写setter和getter方法,一般的话,我们大概都是使用懒加载方法,然后重写getter方法,做一个非空判断.然后有时候根据 ...
- ES5 的 setter 和 getter
有两种方式使用 setter 和 getter 1. set/get var person = { _name: '', get name() { return this._name }, set n ...
- OC中实例变量可见度、setter、getter方法和自定义初始化方法
在对类和对象有一定了解之后,我们进一步探讨实例变量的可见度等相关知识 实例变量的可见度分为三种情况:public(共有),protected(受保护的,默认),private(私有的),具体的不同和特 ...
- ios特性访问器方法(setter和getter)
Employee.h @interface Employee:NSObject { int _employeeNumber; NSString *_name; Employee*_supervisit ...
- 如果将synthesize省略,语义特性声明为assign retain copy时,自己实现setter和getter方法
如果将synthesize省略,并且我们自己实现setter和getter方法时,系统就不会生成对应的setter和getter方法,还有实例变量 1,当把语义特性声明为assign时,setter和 ...
- form与action之setter与getter(转)
对于表单提交数据给action时候,可以简单的用setter与getter函数实现值的传递. 例如在jsp里有这么个form: <s:form action="login"& ...
- 【Java基础】setter与getter方法
//下面代码实现设置和获取学生姓名和成绩. class lesson5homework { public static void main(String[] args) { TestCode TC=n ...
- 假设将synthesize省略,语义特性声明为assign retain copy时,自己实现setter和getter方法
假设将synthesize省略,而且我们自己实现setter和getter方法时,系统就不会生成相应的setter和getter方法,还有实例变量 1,当把语义特性声明为assign时,setter和 ...
随机推荐
- HTTP&&Fiddler教程
很不错的学习资料! HTTP http://www.cnblogs.com/TankXiao/category/415412.html http://www.cnblogs.com/TankXiao/ ...
- python 迭代器和生成器详解
一.迭代器 说迭代器之前有两个相关的名词需要介绍:可迭代对象:只要定义了__iter__()方法,我们就说该对象是可迭代对象,并且可迭代对象能提供迭代器.迭代器:实现了__next__()或者next ...
- CCF_ 201403-4_无线网络
分散点的bfs,先建立一个互相是否可达的二维数组,vis[i][j]代表到第i个点,走了j步的状态,注意判断新增路由器数量是否超过K. #include<cstdio> #include& ...
- Go语言实现:【剑指offer】合并两个排序的链表
该题目来源于牛客网<剑指offer>专题. 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. Go语言实现: //递归 func merge(l ...
- Jmeter之存储测试结果
前言 Jmeter做接口测试时,发送请求后,我们可以通过察看结果树看到结果,但是如果想要把测试结果保存起来,那该怎么做呢? 一:添加一个登录的http请求,填入正确的ip,接口地址,以及参数 二:右键 ...
- Optional类包含的方法介绍及其示例
Optional类的介绍 javadoc中的介绍 这是一个可以为null的容器对象.如果值存在则isPresent()方法会返回true,调用get()方法会返回> 该对象. 使用场景 用于避免 ...
- 对C语言整数类型的一点理解
作者:autogeek 原文链接:http://www.cnblogs.com/autogeek/p/4321635.html 1.先从一个列子引出问题: //sample_1 unsigned ch ...
- 珠峰-node
##### 文件流的读写. ##### 文件流对pipe的封装. ####
- android应用开发错误:Your project contains error(s),please fix them before running your
重新打开ECLIPSE运行android项目,或者一段时间为运行ECLIPSE,打开后,发现新建项目都有红叉,以前的项目重新编译也有这问题,上网搜索按下面操作解决了问题 工程上有红叉,不知道少了什么, ...
- 《果壳中的C# C# 5.0 权威指南》 - 学习笔记
<果壳中的C# C# 5.0 权威指南> ========== ========== ==========[作者] (美) Joseph Albahari (美) Ben Albahari ...