2016-05-14 8 views
0

in C++:Wie macht PHP kompsition?

class first{ 
    int x; 
    first(); 
} 

class tow{ 
    int y; 
    first fst; 
    tow(); 
} 

class three{ 
    int z; 
    tow tw; 
    three(); 
} 

void main(){ 
    three obj = new three; 
    int var=obj . tw . fst . x; //how I can access to x from a obj ?? 
} 

meine Frage: Was ist die Syntax von PHP zu Datenelement zuzugreifen, wie (x) von einer Instanz von der Klasse wie (drei) ???

+0

Sie müssen stattdessen den PHP-Code, den Sie für Ihre Klassen haben, posten; Die Antwort hängt davon ab, wie Ihre Eigenschaften deklariert werden. – jeroen

+0

Wenn Sie die gleichen Klassen und Objektnamen in PHP hätten, wäre es $ obj-> tw-> fst-> x –

+0

ok vielen Dank Andrew es funktioniert wie du gesagt hast –

Antwort

1

ist das, was Sie suchen:

<? 

class first{ 
    public $x; 

    public function __construct(){ 
     $this->x = 42; 
    } 

} 

class tow{ 
    public $y; 
    public $fst; 

    public function __construct(){ 
     $this->fst = new first; 
    } 

} 

class three{ 
    public $z; 
    public $tw; 

    public function __construct(){ 
     $this->tw = new tow; 
    } 
} 

$obj = new three; 
// assuming there is a constructors that fill eveyhing correct. 
$var = $obj->tw->fst->x; 

echo $var . "\n"; 

Hier arbeitet Beispiel.