如何使用扩展功能设置属性?

时间:2014-10-09 17:21:28

标签: php

我不确定我是否正确地提出了问题。我有一个班级的职责是建立一个定义列表。我的一个功能是设置一些目录路径。在设置新对象时,我不确定如何正确扩展到该功能。见下文。

class VM_Definitions
{
    public $root;
    public $ext;
    public $tsk;
    public $gbl;
    public $sys;
    public $sql;
    public $db;

    public function __construct()
    {
        $this->root = $_SERVER['DOCUMENT_ROOT'];

        /*==============================
          COMMONLY USED FILE EXTENSIONS
        ==============================*/
        // Extension for include files
        $this->ext = '.inc.php';
        // Extension for database processing files
        $this->dbe = '.db.php';
        // Extension for task files
        $this->tsk = '.task.php';
        // Extension for global report files
        $this->gbl = 'global.php';
        // Extension for task files
        $sys = '.proc.php';
    }

    public function DB_Directory($root)
    {
        /*==============================
          MAIN DATABASE DIRECTORY PATH
        ==============================*/
        $this->sql = $root.'/db/';
        $this->db = $this->sql;
    }
}
//THIS WORKS FINE
$def = new VM_Definitions;
//THIS DOES NOT
$ms_Database = new VM_Definitions::DB_Directory($def->root);

1 个答案:

答案 0 :(得分:1)

您可以更改DB_Directory方法以返回一些内容,在本例中为$ this-> db,它将存储在$ ms_Database变量中。

 public function DB_Directory($root)
{
    /*==============================
      MAIN DATABASE DIRECTORY PATH
    ==============================*/
    $this->sql = $root.'/db/';
    $this->db = $this->sql;

    // Return a value
    return $this->db;
}

然后访问类方法:

$ms_Database = $def->DB_Directory($def->root);

注意双冒号(::)语法用于访问静态类成员,而箭头( - >)用于访问 object (实例化的类)。

查看PHP - Object Oriented Programming以获得优秀的OOP课程。