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和 ...
随机推荐
- A Simple Introduction To Computer Networking
Most networking discussions are a jumble of acronyms. Forget the configuration details - what are th ...
- c/c++判断文件是否存在
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <fstream> #include <cstd ...
- C语言关于getchar()的小笔记
#include <windows.h> #include <mmsystem.h> #include <string.h> void main() { int a ...
- https搭建(自签名证书)
博客搬家: https搭建(自签名证书) 上一篇博客探究了https(ssl)的原理,为了贯彻理论落实于实践的宗旨,本文将记录我搭建https的实操流程,使用Apache2+ubuntu+openss ...
- 检测并移除WMI持久化后门
WMI型后门只能由具有管理员权限的用户运行.WMI后门通常使用powershell编写的,可以直接从新的WMI属性中读取和执行后门代码,给代码加密.通过这种方式攻击者会在系统中安装一个持久性的后门 ...
- 在.NET Core中使用MachineKey
在.NET Core中使用MachineKey 姐妹篇:<ASP.NET Cookie是怎么生成的> 姐妹篇:<.NET Core验证ASP.NET密码> 在上篇文章中,我介绍 ...
- docker集合
docker集合 docker(1):容器技术简介 docker(2):docker的“前身”—lxc docker(3):docker简介 docker(4):docker的安装(centos7)和 ...
- 字符串(Java.lang.String类)的使用
java字符串就是Unicode字符序列,例如"Java"就是四个Unicode字符 java没有内置的字符串类型,而是在标准Java类库中提供了一个预定义的类String.每个用 ...
- Shiro -- (四) 数据库支持
主要就是JdbcRealm这个类 先看一下部分源码: 先建表:users(用户名 / 密码).user_roles(用户 / 角色).roles_permissions(角色 / 权限),并且往use ...
- (四)开源C# WPF控件库《AduSkin – UI》
微信公众号:[Dotnet9的博客],网站:[Dotnet9],问题或建议:[请网站留言], 如果对您有所帮助:[欢迎赞赏]. 开源C# WPF控件库系列: (一)开源C# WPF控件库<Mat ...