2012-10-10 36 views
0

如何通過包含類文件將值傳遞到我的類中,並將它用作該類中的$var在PHP5中?如何將ScriptA中的值傳遞給ScriptB中的類?

到目前爲止的代碼:

// ScriptA 

    $cacheF=test; 

    include("ScriptB_MyClass.php"); 

    $one = new MyClass(array('$cacheF')); 
    ------------ 

    // ScripB_MyClass.php 

    class MyClass { 

     var $cacheF; 

     function __construct() { 
      $this->cacheF = $cacheF; 
      return $this->cacheF = $cacheF; 
     } 

     $the_cache_path="/home/$cacheF/myfile.txt"; 

     // execute other functions 

    } 
+2

我想你應該開始學習PHP的非常基本的http://www.php.net/manual/en/language.variables.basics .php – OcuS

回答

1

之前,我建議你仔細閱讀http://php.net/manual/en/language.oop5.php

然而,從這裏開始......

// Something.php 
<?php 

require "MyClass.php"; 

$cacheFolder = "test"; 

$one = new MyClass($cacheFolder); 
$one->doSomething(); 


// MyClass.php 
<?php 

class MyClass { 

    protected $_cacheFolder; 
    protected $_cachePath; 

    public function __construct($_folder) { 
     $this->_cacheFolder = $_folder; 
     $this->_cachePath = '/home/' . $this->_cacheFolder . '/myfile.txt'; 
    } 

    public function doSomething() { 
     print $this->_cacheFolder . PHP_EOL . $this->_cachePath; 
    } 
} 
+0

謝謝,原來不難理解。現在可以更改另一個變量,例如:protected cache; $ this-> cache = $ this - > _ cachePath; – Pedro

相關問題