草庐IT

春晚魔术

全部标签

php - 在 PHP 中创建和使用魔术方法

我正在努力掌握PHP的神奇方法,为此我创建了一个如下所示的测试类:switchConstruct(func_get_args());}protectedfunctionswitchConstruct(array$args){switch(count($args)){case0:returnprint"0params";case1:returncall_user_func_array(array($this,'constr1'),$args);case2:returncall_user_func_array(array($this,'constr2'),$args);}die("Inva

带有 __get() 魔术方法的 PHP stdClass()

以如下代码为例:classxpto{publicfunction__get($key){return$key;}}functionxpto(){static$instance=null;if(is_null($instance)===true){$instance=newxpto();}return$instance;}echoxpto()->haha;//returns"haha"现在,我正在尝试归档相同的结果,但不必编写xpto类。我猜我应该写这样的东西:functionxpto(){static$instance=null;if(is_null($instance)===true

php 魔术方法 __set

我正在尝试为类设置一个不明确的变量。沿着这些线的东西:values[$key];}function__set($key,$value){$this->values[$key]=$value;}}$user=newMyClass();$myvar="Foo";$user[$myvar]="Bar";?>有没有办法做到这一点? 最佳答案 正如$instance->$property所述(或$instance->{$property}使其跳出)如果你真的想把它作为数组索引来访问,实现ArrayAccess接口(interface)并使用o

php - 使用 __set 和 __get 魔术方法实例化类

我正在自动加载我的类,并且想要一种在使用时动态实例化该类的方法。我不想在我的父类中有20个类实例化,而是想要一种在类被调用时实例化它的方法。例如:$this->template->render();将实例化$this->template=newTemplate();我试过了publicfunction__set($name,$value){return$this->$name;}publicfunction__get($name){$this->$name=new$name();}这似乎行不通,但我也认为我做错了。还有一个我无法弄清楚的问题是我的类驻留在\System命名空间中。我似乎

php - 在 PHP 中使用默认魔术引号或用户定义的 addslash/stripslash 哪个更好?

在PHP中使用默认魔术引号或用户定义的addslash/stripslash哪个更好?我想用最好的。请帮助我。 最佳答案 手动完成!魔术引号在PHP5中被弃用,并且在PHP6中被完全删除。不仅如此,它们还是纯粹的邪恶!它们会在你无法想象的地方造成错误。显式总是比隐式好(正如Python所说)。 关于php-在PHP中使用默认魔术引号或用户定义的addslash/stripslash哪个更好?,我们在StackOverflow上找到一个类似的问题: https:

PHP 魔术方法 __set 和 __get

这样做会被认为是好的做法吗...我的A类具有以下定义:classA{private$_varOne;private$_varTwo;private$_varThree;public$varOne;publicfunction__get($name){$fn_name='get'.$name;if(method_exists($this,$fn_name)){return$this->$fn_name();}elseif(property_exists('DB',$name)){return$this->$name;}else{returnnull;}}publicfunction__s

php - 静态对象中的魔术方法

我正在努力实现这一目标。我有session管理器类,它是我为我的框架开发的。我需要有唯一的sessionkey,所以不要做这样的事情:$_SESSION['foo']=$bar;我这样做:Session::set('foo',$bar);set函数会做这样的事情:$_SESSION[$unique.'foo']=$bar;它很好用,但我想像这样使用它:Session['foo']=$bar或者像这样:Session->foo=$bar我发现我不能在静态对象中使用->,我也不能使用像__set和__get这样的魔法函数。那么,有什么办法可以实现这种行为吗? 最

php - 如何实现_isset魔术方法?

我尝试实现__isset魔术方法比如下面的代码,为什么我总是得到undefinedindex错误?谁能告诉我该怎么做?classc{public$x=array();publicfunction__get($name){return$this->x[$name];//undefinedindex:#1:a/#2:b/#3:d}publicfunction__isset($name){returnisset($this->x[$name]);}}$c=newc;var_dump(isset($c->a));var_dump(isset($c->a->b));#1var_dump(isse

php - __FILE__ Windows 上 PHP 中的魔术常量不返回斜杠

我是PHP的新手。我正在对在Apache中开发和运行的PHP应用程序进行故障排除。我们正在将应用程序移动到在IIS下运行php的Windows服务器。index.php的顶部有以下代码会引发错误:当我使用警报检查__FILE_时,它显示了没有任何斜线的整个路径。';echo'alert("'.__FILE__.'")';echo'';?>所以不是显示c:\directory\subdirectory\theapp\index.php它显示c:directorysubdirectorytheappindex.php我猜这就是我收到以下错误的原因:注意:第3行C:\Directory\su

php - 魔术方法 __call() 参数作为 "real"参数

我正在寻找有关如何使用参数列表动态实例化类的解决方案。例如:classtest{publicfunction__call($name,$args){/*blah,blahclassexists?require*/returnnew$name($args);}publicfunction__toString(){return(string)$this->extension();//toenforce__toStringofextension}}classextensionextendstest{publicfunction__construct(classTypeHint$object,