0

我有功能,说:

//file file1.php 
class p{
  function a(){
    global$v1;        
    $v1='';   
    //some processing to $v1 here//
    return $v1;
  }
}

如何访问 $v1?($v1 in return $v1)

这是我的尝试(在另一个文件中):

//file file2.php
include 'file1.php';
$obj=new p();
$print=$obj->a()->$v1;
echo $print;

为什么不打印任何东西?

4

2 回答 2

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;
于 2013-03-30T12:14:41.040 回答
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;
?>
于 2013-03-30T12:17:54.083 回答