我有功能,说:
//file file1.php
class p{
function a(){
global$v1;
$v1='';
//some processing to $v1 here//
return $v1;
}
}
如何访问$ v1? (return $v1
中的$ v1)
这是我的尝试(在另一个文件中):
//file file2.php
include 'file1.php';
$obj=new p();
$print=$obj->a()->$v1;
echo $print;
为什么不打印任何东西?
答案 0 :(得分:1)
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;
答案 1 :(得分:0)
<?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;
?>