I'm learning classes and got the following problem, sorry if the title is misleading. I'm still missing many terms:
Calling the index method, it sends "hello" to addToSsml(). 'Hello' gets stored in the array 'addToSsml'. After that I'm initializing(?) the Dev-class and call the world-method in it. World method sends "world" to the addToSsml in the parent class.
The problem seems to be: Because of $devclass->method() the parent-constructor gets called again and resets the the array "ssmlArray". The following Output is only "world", not "Hello World".
DevController:
use App\Http\Classes\Dev;
class DevController extends Controller{
public function __construct(){
logger("Constructor called!");
$this->ssmlArray=array();
}
public function index(){
$this->addToSSml("Hello ",false);
$devclass=new Dev;
$devclass->method();
}
public function addToSsml($ssml,$sendToOutput){
#$ssmlArray[]=$ssml;
array_push($this->ssmlArray,$ssml);
logger(json_encode($this->ssmlArray));
if($sendToOutput==true){
$ssml="";
foreach($this->ssmlArray as $singlerow){
$ssml.=$singlerow;
}
print_r(json_encode($ssml));
logger("----");
exit;
}
}
}
Child Class of DevController:
namespace App\Http\Classes;
use App\Http\Controllers\DevController;
class Dev extends DevController{
public function world(){
$this->addToSsml("world",true);
}
}
The childclass calles its parent constructor because it has no own constructor. In general all constructor information need to be accessible from nearly every childclass.
Is the any sollution that the method/function (addToSsml) collects all incomming strings $ssml and if $sendToOutput is true the method should return/output everything collected at once?