草庐IT

private-messaging

全部标签

php - 永久设置私有(private)类变量 PHP

问题:我想在类中永久设置私有(private)变量,然后从类外部使用getter函数访问它们。问题是每次我实例化一个新类并创建一个对象时,它都会破坏先前设置的变量。在提供的示例中,我不想通过调用函数“getAgain”传递对象。我想简单地访问globalVars类而不破坏任何设置变量。我知道通过创建一个“新对象”本质上会破坏当前的非静态变量。所以:如何在类中永久设置私有(private)变量?或如何在不重新实例化类的情况下调用函数(getter/setter)(为了不破坏当前设置的var(s))。我担心我没有以正确的方式处理这个问题,或者我的方法可能有缺陷。";publicfuncti

php - 为什么PHP私有(private)变量在扩展类时公开

我创建了跟随类PHP版本5.5abstractclassModel{var$id;private$cName;private$tName;publicfunction__construct($id=0){$this->cName='ImcName';$this->tName='ImtName';}}然后是扩展类classclaseExtendextendsModel{var$id;publicfunctionhola(){$this->id=1;return(array)$this;}}如果我执行这个:$obj=newclaseExtend();$retHola=$obj->hola(

php - 严重性 : 8192 Message: Methods with the same name as their class will not be constructors in a future version of PHP;

严重性:8192消息:在未来的PHP版本中,与类同名的方法将不再是构造函数;CI_Pagination有一个已弃用的构造函数文件名:libraries/Pagination.php行号:27classCI_Pagination{var$base_url='';//Thepagewearelinkingtovar$total_rows='';//Totalnumberofitems(databaseresults)var$per_page=10;//Maxnumberofitemsyouwantshownperpagevar$num_links=2;//Numberof"digit"li

php - print_r 显示私有(private)变量。为什么?

为什么print_r可以看到私有(private)属性$version,即使它的范围设置为private?classmyClass{private$version;publicfunctionset_version($value){$this->version=$value;}}$class=newmyClass();$class->set_version("1.2");echo"";print_r($class); 最佳答案 print_r()显示用于调试目的的私有(private)成员属性。它不应用于出于显示目的输出对象(例如在

php - 在静态函数中访问公共(public)/私有(private)函数?

由于您不能不在静态函数中使用$this->,您应该如何访问静态函数中的常规函数​​?privatefunctionhey(){return'hello';}publicfinalstaticfunctionget(){return$this->hey();}这会引发错误,因为您不能在静态中使用$this->。privatefunctionhey(){return'hello';}publicfinalstaticfunctionget(){returnself::hey();}这会引发以下错误:Non-staticmethodVote::get()shouldnotbecalledst

php - Laravel 私有(private)变量在 Controller 中的两个方法之间共享

如何在LaravelController中使用私有(private)变量,并在两个方法之间共享该变量值。(在一个中设置它在另一个中使用它)。 最佳答案 您是在谈论一个Controller,对吗?所以我假设这就是你的意思:classControllerControllerextendsController{private$variable;publicfunction__construct($whatever){$this->variable=$whatever;}publicfunctionmethod1($newValue){$t

php - 在扩展类(class)中调用私有(private)电话?

我有一个包含函数funcB()的父类,我想通过在此函数中做一些更改来用更好的函数覆盖它。父类中的这个函数调用同一个类中的另一个私有(private)函数。示例代码:classclassA{privatefunctionfuncA(){return"funcAcalled";}publicfunctionfuncB(){$result=$this->funcA();return$result;}}classClassBextendsClassA{publicfunctionfuncB($a){//dosomemorestuff$result=$this->funcA();return$r

PHP - 何时在类中使用私有(private)静态属性

我正在学习php,但在这门语言中我还有很多不清楚的地方。我想知道我们什么时候以及为什么要在类中使用privatestaticproperties。据我了解,私有(private)属性只能由定义它的类访问。所以,私处明了,静处还不清楚。在文档中它说:Declaringclasspropertiesormethodsasstaticmakesthemaccessiblewithoutneedinganinstantiationoftheclass.Apropertydeclaredasstaticcannotbeaccessedwithaninstantiatedclassobject(t

php - 如何检测类属性是私有(private)的还是 protected

如何不使用外部库(仅限纯PHP)检测类属性是私有(private)的还是protected?如何检查是否可以从类外部设置属性? 最佳答案 使用Reflection.getProperty('foo');var_dump($prop->isPrivate());$prop=$reflector->getProperty('bar');var_dump($prop->isPrivate());?> 关于php-如何检测类属性是私有(private)的还是protected,我们在StackO

php - 从继承类调用私有(private)方法

我想在我的简单ORM中用PHP实现一个钩子(Hook)系统:classRecord{publicfunctionsave(){if(method_exists($this,"before_save")){$this->before_save();}//...Storingrecordetc.}}classPaymentextendsRecord{privatefunctionbefore_save(){$this->payed_at=time();}}$payment=newPayment();$payment->save();这会导致fatalerror:Fatalerror:Cal