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和 ...
随机推荐
- C++ 中库函数bsearch的简单研究(含示例)
/**//*bsearch函数声明如下: void *bsearch(const void *key, const void *base, size_t *nelem, ...
- 为了不复制粘贴,我被逼着学会了JAVA爬虫
整理了一些Java方面的架构.面试资料(微服务.集群.分布式.中间件等),有需要的小伙伴可以关注公众号[程序员内点事],无套路自行领取 本文作者:程序员内点事 更多精选 技术部突然宣布:JAVA开发人 ...
- re模块 findall()详解
1. findall() 函数的2种表示形式 import re kk = re.compile(r'\d+') kk.findall('one1two2three3four4') #[1,2,3,4 ...
- Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest
Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest(dp+线段树) 题目链接 题意: 给定3个人互不相同的多个数字,可以 ...
- Git 分支设计规范
概述 这篇文章分享 Git 分支设计规范,目的是提供给研发人员做参考. 规范是死的,人是活的,希望自己定的规范,不要被打脸. 在说 Git 分支规范之前,先说下在系统开发过程中常用的环境. 简称 全称 ...
- 提升命令行效率的Bash快捷键
转自:http://linuxtoy.org/archives/bash-shortcuts.html 生活在 Bash shell 中,熟记以下快捷键,将极大的提高你的命令行操作效率. 大部分对其他 ...
- Flutter 基础布局Widgets之Align
Align的作用是为了设置子child的对齐方式,一般作为其他控件的一个参数. 构造函数 const Align({ Key key, this.alignment = Alignment.cente ...
- 20200115--python学习第九天
今日内容 三元运算 函数 考试题 1.三元运算(又称三目运算) v= 前面 if 条件 else 后面 if 条件: v = '前面' else: v ='后面' 示例:让用户输入值,如果值是整数,则 ...
- 获取出口IP地址
curl https://www.ipaddress.com/ |grep "My IPv4 Address" # 推荐 curl icanhazip.com curl www.t ...
- java连接Oracle数据库,从ResultSet中提取数据出现java.sql.sqlException结果集已耗尽
出现错误的原因是ResultSet中并没有任何东西,再调用next()方法就会出错,原因可能是oracle创建用户,表没有提交,commit即可