草庐IT

this-reference

全部标签

PHP:str_replace() 中的 "... variables can be passed by reference"?

我创建了一个函数来打印一个包含变量的prepared-statement-sql-string,基于我在另一个StackOverflowquestion中找到的内容.这是我的代码:foreach($paramsas$idx=>$param){if($idx==0)continue;$sql=str_replace('?',"'".$param."'",$sql,1);}printError($sql);当我运行它时,我得到:fatalerror:第3行只能通过引用传递变量。但是当我使用$sql=preg_replace('/\?/',"'".$param."'",$sql,1);对于第

PHP:str_replace() 中的 "... variables can be passed by reference"?

我创建了一个函数来打印一个包含变量的prepared-statement-sql-string,基于我在另一个StackOverflowquestion中找到的内容.这是我的代码:foreach($paramsas$idx=>$param){if($idx==0)continue;$sql=str_replace('?',"'".$param."'",$sql,1);}printError($sql);当我运行它时,我得到:fatalerror:第3行只能通过引用传递变量。但是当我使用$sql=preg_replace('/\?/',"'".$param."'",$sql,1);对于第

PHP $this 变量

我正在阅读一些我无法理解的PHP代码:classfoo{functionselect($p1,$dbh=null){if(is_null($dbh))$dbh=$this->dbh;return;}functionget(){return$this->dbh;}}我在类中找不到$this->dbh($dbh)声明。我的问题是:$this->dbh的值是多少?它是函数select()的局部变量吗?$this是否属于classfoo的数据成员?为什么这个类中没有$dbh的声明? 最佳答案 PHP对声明并不严格。$this->dbh是一个

PHP $this 变量

我正在阅读一些我无法理解的PHP代码:classfoo{functionselect($p1,$dbh=null){if(is_null($dbh))$dbh=$this->dbh;return;}functionget(){return$this->dbh;}}我在类中找不到$this->dbh($dbh)声明。我的问题是:$this->dbh的值是多少?它是函数select()的局部变量吗?$this是否属于classfoo的数据成员?为什么这个类中没有$dbh的声明? 最佳答案 PHP对声明并不严格。$this->dbh是一个

php - 为什么会出现错误 "expected to be a reference, value given"?

当我尝试通过引用调用带有参数的函数时它会触发functiontest(&$a)...通过call_user_func('test',$b); 最佳答案 call_user_func可以仅按值传递参数,不能按引用传递。如果想传引用,需要直接调用函数,或者使用call_user_func_array,它接受引用(但是这可能不适用于PHP5.3及更高版本,具体取决于手册的哪一部分)。 关于php-为什么会出现错误"expectedtobeareference,valuegiven"?,我们在S

php - 为什么会出现错误 "expected to be a reference, value given"?

当我尝试通过引用调用带有参数的函数时它会触发functiontest(&$a)...通过call_user_func('test',$b); 最佳答案 call_user_func可以仅按值传递参数,不能按引用传递。如果想传引用,需要直接调用函数,或者使用call_user_func_array,它接受引用(但是这可能不适用于PHP5.3及更高版本,具体取决于手册的哪一部分)。 关于php-为什么会出现错误"expectedtobeareference,valuegiven"?,我们在S

php - 通过 $var::$reference 访问静态变量

我正在尝试通过使用变量类名来访问类中的静态变量。我知道,为了访问类中的function,您使用call_user_func():classfoo{functionbar(){echo'hi';}}$class='foo';call_user_func(array($class,'bar'));//printshi但是,当尝试访问类中的静态变量时,这不起作用:classfoo{publicstatic$bar='hi';}$class="foo";call_user_func(array($class,'bar'));//nothingecho$foo::$bar;//invalid我如

php - 通过 $var::$reference 访问静态变量

我正在尝试通过使用变量类名来访问类中的静态变量。我知道,为了访问类中的function,您使用call_user_func():classfoo{functionbar(){echo'hi';}}$class='foo';call_user_func(array($class,'bar'));//printshi但是,当尝试访问类中的静态变量时,这不起作用:classfoo{publicstatic$bar='hi';}$class="foo";call_user_func(array($class,'bar'));//nothingecho$foo::$bar;//invalid我如

IDEA使用Git提交代码remote: GitLab: You are not allowed to push code to protected branches on this project

创建了一个新项目,提交到GitLab上的master分支时,报错如下:git-ccredential.helper=-ccore.quotepath=false-clog.showSignature=falsepush--progress--porcelainoriginrefs/heads/master:masterremote:GitLab:Youarenotallowedtopushcodetoprotectedbranchesonthisproject.error:failedtopushsomerefsto'https://gitlab.XXXX.git'Tohttps://gitl

php - 将当前对象 ($this) 转换为子类

我有一个类,可能需要将对象更改为更进一步的后代类。这可能吗?我知道一种选择是返回它的副本,但改用子类,但实际上修改当前对象会很好......所以:classmyClass{protected$var;functionmyMethod(){//functionwhichchangestheclassofthisobjectrecast(myChildClass);}}classmyChildClassextendsmyClass{}$obj=newmyClass();$obj->myMethod();get_class_name($obj);//=>myChildClass