2016-08-22 2 views
0

Ich möchte eine Funktion in einer Klasse erstellen, damit ich Zugriff auf Klassenvorfahre bekommen konnte, wo Klassenvorfahre Eltern zu Klasse Vater und Klasse Vater Elternteil ist, um mich zu klassifizieren. Ich möchte das also ausführen, weil die Funktionen in den Klassenvorläufern vom Konstruktorwert der Vorgängerklasse abhängig sind. Und ich möchte nicht den Standard-Konstruktor der Me-Klasse und der Vater-Klasse für diesen Prozess verwenden.Wie umgehen Sie den Konstruktor Wert in PHP mit Vererbung?

class ancestor 
 
{ 
 
var $a,$b; 
 
public function __construct($valA,$valB) 
 
{ 
 
    //this constructor's value i want to access 
 
    $this->a=$valA; 
 
    $this->b=$valB; 
 
} 
 
public add() 
 
{ 
 
return($this->a+$this->b); 
 
} 
 

 
} 
 
class dad extends ancestor 
 
{ 
 
public function __construct() 
 
{ 
 
//i don't want to use this as a bridge but i want to handle exception also 
 

 
} 
 

 
} 
 
class me extends dad 
 
{ 
 

 
public function ancestorConstructerValue($a,$b) 
 
{ 
 
//what is the code to access the ancestor's constructor function? 
 
} 
 
} 
 

 
$me= new me; 
 
$me->ancestorConstructerValue("",""); //i want to enter the value through this function 
 
echo $me->add(); 
 
?>

+0

$ args = func_get_args versuchen(); call_user_func_array (array ('parent', '__construct'), $ args); in ancestorConstructerValue –

+0

Ich möchte Zugriff von Ahnen Klasse von mir Klasse, Vorfahren ist nicht Eltern von mir Klasse, es ist Großeltern für mich. –

+0

siehe unten Code es funktioniert einwandfrei –

Antwort

0

Verwendung unter Code: public add() sollte Funktion d.h public function add haben()

class ancestor 
{ 
var $a,$b; 
public function __construct($valA='',$valB='') 
{ 
    //this constructor's value i want to access 
    $this->a=$valA; 
    $this->b=$valB; 
} 
public function add() 
{ 
return($this->a+$this->b); 
} 

} 
class dad extends ancestor 
{ 
public function __construct() 
{ 
//i want to use this as a bridge but i want to handle exception also 
$args = func_get_args(); 
call_user_func_array(array('parent', '__construct'), $args); 
} 

} 
class me extends dad 
{ 

public function ancestorConstructerValue($a,$b) 
{ 
//what is the code to access the ancestor's constructor function? 
$args = func_get_args(); 
call_user_func_array(array('parent', '__construct'), $args); 
} 
} 

$me= new me; 
$me->ancestorConstructerValue(1,2); //i want to enter the value through this function 
echo $me->add(); 
+0

ist dies möglich, ohne den Konstruktor von Papa zu nehmen? Wenn ich einen anderen Set-Konstruktor in der Klasse Vater verwenden möchte, würde dieser Code das beeinflussen? –

+0

nup, zuerst versuchen, was auch immer Ihre Sorge, d. H. Ich meine, ändern Sie es entsprechend. und lassen Sie mich dies Ihre Frage gelöst für das Hinzufügen von Werten –

+0

Durch den Dank, ich habe die Lösung wegen dir herausgefunden ... –