0

如何通过包含类文件将值传递给我的类并将其用作$varPHP5 中的该类内部?

到目前为止的代码:

    // 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

    }
4

1 回答 1

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;
    }
}
于 2012-10-10T12:33:36.800 回答