2017-03-16 14 views
0

classコンストラクタのaプロパティを設定することは、PHP 7.0.17を使用しても動作しません。PHPコンストラクタがクラス内で呼び出されていない

class T 
{ 
    public $property; 
    function __contruct() 
    { 
     $this->property = "Test"; 
     print "I'm called :)"; 
    } 
} 

$t1 = new T(); 
print_r($t1); 

t.php

私はphp t.php経由でコードを実行すると、私が手:明らか

T Object 
(
    [property] => 
) 

、私は端末に出力し、プロパティを設定することにI'm called :)を期待しますTest。図解されている通りin this example on php.net

これは非常に簡単な問題ですが、私は本当にこれについて別のペアが必要です。

+0

'ます$ this-> property'、ない' $この - > $ property' –

+0

'$この - > $ property' - > '$'が2回あることに注意してください。それは意図されていますか? – Mjh

+3

また、 '__contruct'ではなく' __construct'です。 – roberto06

答えて

2

変更$this->$propertycontruct

constructから
class T 
{ 
    public $property; 
    function __construct() 
    { 
     $this->property = "Test"; 
     print "I'm called :)"; 
    } 
} 

$t1 = new T(); 
print_r($t1); 
+3

* 'contruct'の入力ミスにも言及しています* – Jer

+0

私はあなたの答えを受け入れることができるまで8分待たなければなりません。 – dotnetCarpenter

+0

@ C0dekidありがとう.. –

0

変更

$this->propertyへ( this含む)オブジェクトとクラスのプロパティにアクセスするとき、あなたは再び $を使用するように持っていけません。クラスの外から

$this->property='something'; 

ある

$t = new T(); 
$t->property='something'; 
関連する問題