简单的PHP类问题

时间:2009-08-19 23:44:40

标签: php oop

所以我有:

class foo {
  public $variable_one;
  public $variable_two;

  function_A(){
    // Do something
    $variable_one;
    $variable_two;

    // If I define variable_3 here!
    $variable_3
    // Would I be able to access it in function_B?
  }

  function_B(){
    // Do something
    $variable_4 = $variable_3
  }
}


$myObj = new foo();
// Now what do I write in order to assign "variable_one" and "two" some value?
$myObj->$variable_one = 'some_value' ??
$myObj->$variable_two = 'some_value' ??

5 个答案:

答案 0 :(得分:7)

首先,当您在$variable_one;内简单地编写A()时,会引用您班级的成员变量!这将是一个完全不同的,新创建的局部变量,名为$variable_one,与类变量无关。

相反,你想要:

function A() {
    $this->variable_one;
}

其次,您的$variable_3也是一个本地变量,可以在任何其他函数中访问。

第三,你在底部的作业在形式上是正确的,但在语法上则不正确:那里有一个额外的$。你想要:

$myObj->variable_one = 'some value';

答案 1 :(得分:2)

不,$variable_3已在function_A范围内创建(并将被销毁)。这是由于功能范围。

http://us3.php.net/manual/en/language.variables.scope.php

如果你想在执行离开function_A的范围时你的对象保留$ variable_3,你需要将它指定为类属性,类似于$ variable_1和$ variable2。

class YourClass
{

    public $variable_1;
    public $variable_2;
    public $variable_3;

    function_A()
    {
        $this->variable_3 = "some value"; // assign to the object property
        $variable_4 = "another value"; // will be local to this method only
    }

    function_B()
    {
        echo $this->variable_3; // Would output "some value"
        echo $variable_4; // var does not exist within the scope of function_B
    }

}

答案 2 :(得分:1)

$myObj->variable_one = aValue;
$myObj->variable_two = anotherValue;

答案 3 :(得分:1)

正确的代码如下(见评论中的答案)

class foo {
  public $variable_one;
  public $variable_two;
  private $variable_three; // private because it is only used within the class

  function _A(){
    // using $this-> because you want to use the value you assigned  at the
    // bottom of the script. If you do not use $this-> in front of the variable, 
    // it will be a local variable, which means it will be only available inside
    // the current function which in this case is _A
    $this->variable_one; 
    $this->variable_two;

    // You need to use $this-> here as well because the variable_3 is used in
    // function _B
    $this->variable_3;
  }

  function _B(){
    // Do something
    $variable_4 = $this->variable_3
  }
}


$myObj = new foo();
$myObj->variable_one = 'some_value1'; // Notice no $ in front of the var name
$myObj->variable_two = 'some_value2'; // Notice no $ in front of the var name

必须使用$ this->访问类变量(属性)。前缀,除非它们是静态的(在你的例子中它们不是)。如果您不使用前缀$ this->它们将是您定义它们的函数中的局部变量。

我希望这有帮助!

答案 4 :(得分:0)

如果variable_onevariable_twopublic,您可以按照指定分配它们(只需删除“$”...所以$ classObject-> variable_one)。通常,您希望通过将变量设置为受保护或私有来encapsulate变量:

class MyClass
{
    protected $_variable_one;

    public function getVariableOne()
    {
        return $this->_variable_one;
    }

    public function setVariableOne($value)
    {
        $this->_variable_one = $value;
    }
}

$c = new MyClass();
$c->setVariableOne("hello!");

echo $c->getVariableOne(); // hello!