2009-08-19 17 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'は '$ variable_one == 'foo''ならば' $ myObj-> foo'です。 – p4bl0

+0

$ myObj-> variable_one = '値を何かに割り当てる'; – localshred

答えて

7

まず、あなたは、単に$variable_one;A()内部を書くとき、それはないを行うには、あなたのクラスのメンバ変数を参照してください!これは、クラス変数とは関係のない、全く異なる、新しく作成された$variable_oneというローカル変数です。

代わりに、あなたがしたい:

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

第二に、あなたの$variable_3は、ローカル変数であり、は、他の関数でアクセスすることはできません。

第3に、下部の割り当ては正しい形式ではありますが、構文では正しくありません。そこに余分な$があります。あなたが欲しい:publicです

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

いいえ、$variable_3function_Aの範囲で作成された(と破壊されます)。これは機能範囲によるものです。

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

あなたは実行がfunction_Aの範囲を離れると、あなたのオブジェクトによって保持されるように$ variable_3をご希望の場合は、$ 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_one場合とvariable_two、あなたが指定されているようにそれらを割り当てることができます(単に "$" を削除...そう$ 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->を使用しない場合は、それらを定義する関数内のローカル変数になります。

こちらがお役に立てば幸いです。

関連する問題