[英]php how to access variable inside function
我有功能,说:
//file file1.php
class p{
function a(){
global$v1;
$v1='';
//some processing to $v1 here//
return $v1;
}
}
如何访问$ v1? ($ v1作为return $v1
)
这是我的尝试(在另一个文件中):
//file file2.php
include 'file1.php';
$obj=new p();
$print=$obj->a()->$v1;
echo $print;
为什么不打印任何内容?
class p{
function a($v1){
///some processing to $v1 here//
return $v1;
}
}
然后将变量作为参数传递给函数。
//file file2.php
$v1 = 'Test';
require 'file1.php';
$obj=new p();
$print=$obj->a($v1);
echo $print;
<?php
//file file1.php
class p{
protected $v1;
public function setP($v1){
$this->v1 = $v1;
}
public function getP(){
$this->setP('');
///some processing to $v1 here//
return $this->v1;
}
}
$obj = new p();
$print = $obj->getP();
echo $print;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.