首页 后端开发 php教程 详解php的魔术方法__get()和__set()使用介绍_PHP教程

详解php的魔术方法__get()和__set()使用介绍_PHP教程

Jul 21, 2016 pm 03:16 PM
get php run set w 介绍 使用 官方 文档 方法 解释 详解 魔术

先看看php官方文档的解释:
__set() is run when writing data to inaccessible properties.
__get() is utilized for reading data from inaccessible properties.

究竟用中文怎么翻译呢?
inaccessible :n. 难达到;难接近;无法理解。

有代码有真相:

复制代码 代码如下:

error_reporting(E_ALL);
class stu{
private $a;
private $b = 0;
public $c;
public $d = 0;
//这里的 private 可以用 protected public 替代
private function __get($name) {
return 123;
}

//这里的 private 也可以用 protected public 替代
private function __set($name, $value) {
echo "This is set function";
}
}
$s = new stu();
var_dump($s->a); //output: 123
var_dump($s->b); //output: 123
var_dump($s->c); //output: null
var_dump($s->d); //output: 0
var_dump($s->e); //output: 123
$s->a = 3; //output: This is set function
$s->c = 3; //no output
$s->f = 3; //output: This is set function
?>


结果分析:
如果没有__get方法,执行 var_dump($s->a) var_dump($s->b) 会有致命的错误
如果没有__get方法,执行 var_dump($s->e) 会有一个notice,提示没有定义属性 $e

总结:
1. 从一个难以访问的属性读取数据的时候 __get() 方法被调用
2. 向一个难以访问的属性赋值的时候 __set() 方法被调用
3. 难以访问包括:(1)私有属性,(2)没有初始化的属性
4. __isset() __unset() 也类似

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/325964.htmlTechArticle先看看php官方文档的解释: __set() is run when writing data to inaccessible properties. __get() is utilized for reading data from inaccessible properties. 究竟用中文怎么...
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

PHP教程
1596
276
Edge PDF查看器不起作用 Edge PDF查看器不起作用 Aug 07, 2025 pm 04:36 PM

testthepdfinanotherapptoderineiftheissueiswiththefileoredge.2.enablethebuilt inpdfviewerbyTurningOff“ eflblyopenpenpenpenpenpdffilesexternally”和“ downloadpdffiles” inedgesettings.3.clearbrowsingdatainclorwearbrowsingdataincludingcookiesandcachedcachedfileresteroresoreloresorelorsolesoresolesoresolvereresoreorsolvereresoreolversorelesoresolvererverenn

VS代码快捷方式专注于Explorer面板 VS代码快捷方式专注于Explorer面板 Aug 08, 2025 am 04:00 AM

VSCode中可通过快捷键快速切换面板与编辑区。要跳转至左侧资源管理器面板,使用Ctrl Shift E(Windows/Linux)或Cmd Shift E(Mac);返回编辑区可用Ctrl `或Esc或Ctrl 1~9。相比鼠标操作,键盘快捷键更高效且不打断编码节奏。其他技巧包括:Ctrl KCtrl E聚焦搜索框,F2重命名文件,Delete删除文件,Enter打开文件,方向键展开/收起文件夹。

修复:Windows Update无法安装 修复:Windows Update无法安装 Aug 08, 2025 pm 04:16 PM

runthewindowsupdatetrubloubleshooterviaSettings>更新&安全> is esseShootsoAtomationfixCommonissues.2.ResetWindowSupDateComponentsByStoppingRealatedServices,RenamingTheSoftWaredWaredWaredSoftwaredSistribution andCatroot2Folders,intrestrestartingthertingthertingtherserviceSteStoceTocle

比较和对比PHP特征,抽象类别和界面与实际用例。 比较和对比PHP特征,抽象类别和界面与实际用例。 Aug 11, 2025 pm 11:17 PM

Useinterfacestodefinecontractsforunrelatedclasses,ensuringtheyimplementspecificmethods;2.Useabstractclassestosharecommonlogicamongrelatedclasseswhileenforcinginheritance;3.Usetraitstoreuseutilitycodeacrossunrelatedclasseswithoutinheritance,promotingD

如何使用PHP中的阵列 如何使用PHP中的阵列 Aug 20, 2025 pm 07:01 PM

phparrayshandledatAcollectionsefefityIndexedorassociativuctures; hearecreatedWithArray()或[],访问decessedviakeys,modifybyAssignment,iteratifybyAssign,iteratedwithforeach,andManipulationUsfunsionsFunctionsLikeCountLikeCountLikeCountLikeCountLikecount()

修复:以太网'身份不明网络” 修复:以太网'身份不明网络” Aug 12, 2025 pm 01:53 PM

Restartyourrouterandcomputertoresolvetemporaryglitches.2.RuntheNetworkTroubleshooterviathesystemtraytoautomaticallyfixcommonissues.3.RenewtheIPaddressusingCommandPromptasadministratorbyrunningipconfig/release,ipconfig/renew,netshwinsockreset,andnetsh

描述观察者的设计模式及其在PHP中的实现。 描述观察者的设计模式及其在PHP中的实现。 Aug 15, 2025 pm 01:54 PM

TheObserverdesignpatternenablesautomaticnotificationofdependentobjectswhenasubject'sstatechanges.1)Itdefinesaone-to-manydependencybetweenobjects;2)Thesubjectmaintainsalistofobserversandnotifiesthemviaacommoninterface;3)Observersimplementanupdatemetho

如何在php中使用$ _cookie变量 如何在php中使用$ _cookie变量 Aug 20, 2025 pm 07:00 PM

$_COOKIEisaPHPsuperglobalforaccessingcookiessentbythebrowser;cookiesaresetusingsetcookie()beforeoutput,readvia$_COOKIE['name'],updatedbyresendingwithnewvalues,anddeletedbysettinganexpiredtimestamp,withsecuritybestpracticesincludinghttponly,secureflag

See all articles