2009-08-19 96 views
2

所以我必須:簡單的PHP類問題

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' ?? 
+0

訪問公共屬性的語法是'$ myObj-> variable_one',而'$ myObj-> $ variable_one'是'$ myObj-> foo' if if $ variable_one =='foo''。 – p4bl0 2009-08-19 23:51:11

+0

$ myObj-> variable_one ='將值賦給某物'; – localshred 2009-08-19 23:56:50

回答

7

首先,當你寫簡單$variable_one;A()它並沒有到您的類的成員變量!這將是一個完全不同的新創建的局部變量,稱爲$variable_one,它與類變量無關。

相反,你想:

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

其次,你的$variable_3也是一個局部變量,並且將在任何其他功能進行訪問。

第三,您在底部的作業在形式上是正確的,但在語法上不正確:其中有一個額外的$。你想:

$myObj->variable_one = 'some value'; 
2

沒有,$variable_3function_A範圍創建(和將被銷燬)。這是由於功能範圍。

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

如果您想$ variable_3的通過你的對象一旦執行離開function_A的範圍被保留,您需要將其指定爲一個類屬性,類似於$ variable_1和$變量2。

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 
    } 

} 
1
$myObj->variable_one = aValue; 
$myObj->variable_two = anotherValue; 
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! 
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->,它們將是您定義它們的函數中的局部變量。

我希望這有助於!