Given the following code I'd like to add strings to the array ssmlArray via addToOutput method. The problem I'm facing (please correct me if I'm wrong) seems to be when calling $devclass->world(); the parent constructor get also called, resets ssmlArray and the only element within the array is "World".
$dev=new DevController;
$dev->index();
class DevController{
public function __construct(){
$this->ssmlArray=array();
}
public function index(){
$this->addToOutput("Hello ",false);
$devclass=new ChildClass;
$devclass->world();
}
public function addToOutput($ssml,$sendToOutput){
array_push($this->ssmlArray,$ssml);
if($sendToOutput==true){
$ssml="";
foreach($this->ssmlArray as $singlerow){
$ssml.=$singlerow;
}
print_r(json_encode($ssml));
exit;
}
}
}
#Childclass
class ChildClass extends DevController{
public function world(){
$this->addToOutput("world",true);
}
}
I want to output the entire array as a string at once (only once print_r) and it needs to be fillable from child classes without clearing the contents. Is that even possible?
When you create ChildClass, it calls the parent's __construct if it doesn't have its own __construct. Therefore, $ssmlArray is created again. You can convert it to static variable.
Check it:
<?php
$dev=new DevController;
$dev->index();
class DevController{
protected static $ssmlArray = [];
public function __construct(){
}
public function index(){
$this->addToOutput("Hello ",false);
$devclass=new ChildClass;
$devclass->world();
}
public function addToOutput($ssml,$sendToOutput){
array_push(self::$ssmlArray,$ssml);
if($sendToOutput==true){
$ssml="";
foreach(self::$ssmlArray as $singlerow){
$ssml.=$singlerow;
}
print_r(json_encode($ssml));
exit;
}
}
}
#Childclass
class ChildClass extends DevController{
/*public function __construct(){
parent::__construct();
}*/
public function world(){
$this->addToOutput("world",true);
}
}
?>
Demo: https://paiza.io/projects/ZQtGPvPYi8mxNqoitHqS8A
Related
I have a base class which sets up's other extending controllers like this:
class BaseController extends Controller
{
public $globalCurrencies;
public $globalLanguages;
public function __construct()
{
$this->globalCurrencies = $this->getCurrencies(); // this works
$this->globalLanguages = $this->getLanguages(); // this works
}
}
And I use one of helpers to extend this class like this:
class SessionHelper extends BaseController
{
public $test;
public function __construct()
{
parent::__construct(); // fire parent aka basecontroller construct
$this->test = $this->globalCurrencies; // this works (variables are set)
echo '__construct: '.$this->test; // this even displays it
}
public function getCurrencies()
{
dd('method'.$this->test); // NOT WORKING
}
public function getCurrentCurrency()
{
return $this->getCurrencies()->where('id', Session::get('currencyId'))->first() ?? null;
}
}
Later on code is used in model:
class Product extends Model
{
protected $table = "products";
public $timestamps = true;
public $sessionHelper;
public function __construct()
{
$this->sessionHelper = new SessionHelper;
}
public function getPrice($conversion_rate = null)
{
return number_format($this->price_retail / $this->sessionHelper->getCurrentCurrency()->conversion_rate, 2);
}
}
Have any body idea why I can access in construct variable but not in method? If i remember correctly construct is fired first so everything after should have access to it.
Declare $test variable as private out side the constructor. Inside the constructor keep it the way you are doing it right now and then make a setter and getter for the test variable.
class testObject
{
private $test;
function __construct($test)
{
$this->test= $this->globalCurrencies;
}
// test getter
function getTest()
{
return $this->test;
}
}
Change your method to be;
public function getCurrencies()
{
dd('method', $this->test);
}
You can not concatenate strings and objects/arrays.
If that doesn't resolve the issue - check the laravel.log
i am working on PHP inheritance(just started leraning PHP). I found that base class methods does not display the value of properties when accessed with a child class object. My code look like this.
<?php
class Base
{
public $pr1;
public $pr2;
function __construct()
{
print "In Base class<br>";
}
public function setPropertie($pr1,$pr2)
{
$this->$pr1=$pr1;
$this->$pr2=$pr2;
}
public function display(){
echo "propertie1".$this->pr1."<br>";
echo "propertie2".$this->pr2."<br>";
}
function __destruct()
{
print "Destroying Baseclass<br>";
}
}
class Child extends Base
{
function __construct()
{
parent::__construct();
print "In Subclass<br>";
}
function __destruct()
{
print "Destroying Subclass<br>";
}
}
$obj=new Child();
$obj->setPropertie('Abhijith',22);
$obj->display();
?>
I can't find what is the problem in the code. How to fix this problem?
You are accessing property incorrectly inside the setPropertie() method. Remove $ from both $pr1 and $pr2 property to access them
Wrong way
$this->$pr1=$pr1;
$this->$pr2=$pr2;
Correct way
$this->pr1=$pr1;
$this->pr2=$pr2;
I have two classes. A child and a parent. The parent is calling a static method from the child (that's an overriden static parent method in the child class) and I get a general server error.
When I remove the relation ('extends' part), all is fine and get no errors. No idea why. Can't you override static methods? Looked for answers but can't seem to find them.
Class Fase {
public static function getbyId($id) {
//some stuff
}
public function getsomefaseitem($fase_item_id) {
FaseItem::getbyid($fase_item_id);
}
}
Class FaseItem extends Fase {
public static function getbyId($id) {
}
}
It works for me.
This seems weird, though. The base should have no knowledge of the derived.
Perhaps use static:: instead and rely on overriding static member functions — or "late static binding". You'll need PHP 5.3 for this.
<?php
class Fase {
public static function getbyId($id) {
echo "Fase::getbyId\n";
}
public function getsomefaseitem($fase_item_id) {
static::getbyid($fase_item_id); // <---
}
}
class FaseItem extends Fase {
public static function getbyId($id) {
echo "FaseItem::getbyId\n";
}
}
$f = new Fase();
$f->getsomefaseitem(0);
?>
How can I prevent the something method below to be created in the foo class ?
class fooBase{
public function something(){
}
}
class foo extends fooBase{
public function __construct(){
echo $this->something(); // <- should be the parent class method
}
public function something(){
// this method should not be allowed to be created
}
}
Use the final keyword (like in Java etc):
class fooBase{
final public function something(){
}
}
class foo extends fooBase{
public function __construct(){
echo $this->something(); // <- should be the parent class method
}
public function something(){
// this method should not be allowed to be created
}
}
See PHP Final keyword. Note that foo will still have a method something, but something will only come from fooBase and foo can't override it.
Use the final keyword.
In your parent:
final public function something()
You can use final to prevent base methods being overwritten.
class fooBase{
final public function something(){
}
}
I have parent and child classes as follows:
abstract class ParentObj {
private $data;
public function __construct(){
$this->data = array(1,2,3);
var_dump($this->data);
$this->method();
}
public function method(){
echo "ParentObj::method()";
}
}
class ChildObj extends ParentObj {
public function __construct(){
parent::__construct();
var_dump($this->data);
}
public function method(){
echo "ChildObj::method()";
}
}
The expected output:
array(1,2,3)
ChildObj::method()
array(1,2,3)
The actual output:
array(1,2,3)
ParentObj::method()
NULL
The problem is, the child object cannot access the data property and the parent refuses to call the overridden method in the child.
Am I doing something wrong, or does anybody have any ideas?
EDIT: I should clarify that I am instantiating a ChildObj as $child = new ChildObj()
You've declared data as private, so ChildObj won't be able to access it - you need to make it protected instead:
protected $data;
My PHP (5.2.8) prints ChildObj::method() - are you running an older version?
Ok, the problem was the methods were actually declared private, not public as in my post, thus suffering the same symptom as the $data property.