<?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的更多相关文章

  1. Python魔术方法-Magic Method

    介绍 在Python中,所有以"__"双下划线包起来的方法,都统称为"Magic Method",例如类的初始化方法 __init__ ,Python中所有的魔 ...

  2. 【原】iOS 同时重写setter和getter时候报错:Use of undeclared identifier '_name';did you mean 'name'

    写了那么多的代码了,平时也没有怎么注意会报这个错误,因为平时都很少同时重写setter和getter方法,一般的话,我们大概都是使用懒加载方法,然后重写getter方法,做一个非空判断.然后有时候根据 ...

  3. ES5 的 setter 和 getter

    有两种方式使用 setter 和 getter 1. set/get var person = { _name: '', get name() { return this._name }, set n ...

  4. OC中实例变量可见度、setter、getter方法和自定义初始化方法

    在对类和对象有一定了解之后,我们进一步探讨实例变量的可见度等相关知识 实例变量的可见度分为三种情况:public(共有),protected(受保护的,默认),private(私有的),具体的不同和特 ...

  5. ios特性访问器方法(setter和getter)

    Employee.h @interface Employee:NSObject { int _employeeNumber; NSString *_name; Employee*_supervisit ...

  6. 如果将synthesize省略,语义特性声明为assign retain copy时,自己实现setter和getter方法

    如果将synthesize省略,并且我们自己实现setter和getter方法时,系统就不会生成对应的setter和getter方法,还有实例变量 1,当把语义特性声明为assign时,setter和 ...

  7. form与action之setter与getter(转)

    对于表单提交数据给action时候,可以简单的用setter与getter函数实现值的传递. 例如在jsp里有这么个form: <s:form action="login"& ...

  8. 【Java基础】setter与getter方法

    //下面代码实现设置和获取学生姓名和成绩. class lesson5homework { public static void main(String[] args) { TestCode TC=n ...

  9. 假设将synthesize省略,语义特性声明为assign retain copy时,自己实现setter和getter方法

    假设将synthesize省略,而且我们自己实现setter和getter方法时,系统就不会生成相应的setter和getter方法,还有实例变量 1,当把语义特性声明为assign时,setter和 ...

随机推荐

  1. SpringMVC基础(二)_文件上传、异常处理、拦截器

    实现文件上传 实现文件上传,需要借助以下两个第三方 jar 包对上传的二进制文件进行解析: commons-fileupload commons-io form表单的 enctype 取值必须为:mu ...

  2. Java入门基础(变量、操作符与表达式)

    Java入门基础 1. 第一个程序 2.变量(命名.运算.整数/小数/字符串.布尔类型) 3.操作符与表达式(算术/逻辑/关系/赋值/自增/类型转换操作符) HelloWorld! public cl ...

  3. HDU_5456_数位dp

    http://acm.hdu.edu.cn/showproblem.php?pid=5456 转化成a=b+c,dp[i][a][b][c]表示剩余i木棒,a是否有进位,b是否首尾,c是否首位,注意每 ...

  4. SpringMVC之Controller层最佳实践

    规范设置接口, 有利于项目的可扩展性,提高前后端的交互特性. 请求参数 响应结果

  5. C# 把带有父子关系的数据转化为------树形结构的数据 ,以及 找出父子级关系的数据中里面的根数据Id

    紧接上一篇,将List<Menu>的扁平结构数据, 转换成树形结构的数据 返回给前端   ,   废话不多说,开撸! --------------------- 步骤: 1. 建 Menu ...

  6. Go语言实现:【剑指offer】旋转数组的最小数字

    该题目来源于牛客网<剑指offer>专题. 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3, ...

  7. Jedis客户端即redis中的pipeline批量操作

    关注公众号:CoderBuff,回复"redis"获取<Redis5.x入门教程>完整版PDF. <Redis5.x入门教程>目录 第一章 · 准备工作 第 ...

  8. 同步锁——ReentrantLock

    本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 Lock接口简介 在JUC包下面有一个java.util ...

  9. 用PHP&JS实现的ID&密码校验程序

    声明:本程序纯粹是本人在学习过程中突发奇想做的,并未考虑任何可行性,实用性,只是留下来供以后参考. 前端页面 sign.html <!DOCTYPE html> <html> ...

  10. .net 微服务实践

    l  前言 本文记录了我的一次.net core 微服务架构实践经验,以及所用到的技术 l  优点 每个服务聚焦于一块业务,无论在开发阶段或是部署阶段都是独立的,更适合被各个小团队开发维护,团队对服务 ...