2012-03-04 73 views
-1

我犯了一个功能,我的类中一样,PHP:如何使用VAR函数外的类

public function admin_ads_manage(){ 

$ads728=$_POST['ads728']; 
$ads600=$_POST['ads600']; 
$ads300=$_POST['ads300']; 



$file728=stripslashes(file_get_contents($this->dir_name."/ads/ads728.txt")); 
$file600=stripslashes(file_get_contents($this->dir_name."/ads/ads600.txt")); 
$file300=stripslashes(file_get_contents($this->dir_name."/ads/ads300.txt")); 

if($_POST['submit']){ 

    $f728=fopen($this->dir_name."/ads/ads728.txt",'w'); 
    $w728=fwrite($f728,$ads728); 

    $f600=fopen($this->dir_name."/ads/ads600.txt",'w'); 
    $w600=fwrite($f600,$ads600); 

    $f300=fopen($this->dir_name."/ads/ads300.txt",'w'); 
    $w300=fwrite($f300,$ads300); 

    echo "<meta http-equiv=\"refresh\" content=\"0\" " ; 


    } 

我想使用的变量(只)这个函数里面外面打印输出中在索引页中,如何访问这个函数只允许使用一些var,而不是执行整个函数

我知道我可以通过$ object-> function()来执行整个函数。 但我只想使用一些变种..........

回答

0
private $myVar; 

public function admin_ads_manage(){ 
    $this->myVar = 'Your value'; 
    // Rest of the code 
} 

public function getMyVar() { 
    return $this->myVar; 
} 

// Where you what to use it 
echo $object->getMyVar(); 
+0

感谢dotoree,我知道这个方法,但我只是问是否有访问外部的功能变种使用它只是没有做其他功能使用它的方法,假设我做了100变种的功能在里面,使100函数使用每个变量只是一个很糟糕的消耗时间,我希望如果我的想法达到了 – 2012-03-04 18:38:09

+0

如果你想改变变量var和变得公开为@Gediminas回答。 – dotoree 2012-03-04 19:04:52

0

创建公共实例变量和访问它。

class A 
{ 
    public $var; 
    public function admin_ads_manage() 
    { 
     //skip some code 
     $this->var = "value to be accessed from outside"; 
    } 
} 
+0

感谢您的帮助,我希望阅读我的回复 – 2012-03-04 18:48:03