Base class function not displaying properties - php

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;

Related

Accessing/Filling PHP array from both partent and child classes

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

How to call a function of child class inside a function of parent class

I want to call a function defined in child class from a function defined in the parent class for some logic. I am getting error of undefined function. How can I call this function. Here is my sample code.
<?php
class First
{
public function __construct()
{
echo "First class is initiated.";
}
public function call_child()
{
$this->get_ouput();
}
}
class Second extends First
{
public function __construct()
{
parent::__construct();
}
public function get_output()
{
echo "Here is your output";
}
}
$obj = new Second();
$obj->call_child();
?>
The issue is spelling mistake in your function call name
<?php
class First
{
public function __construct()
{
echo "First class is initiated.";
}
public function call_child()
{
$this->get_output(); //update the name here
}
}
class Second extends First
{
public function __construct()
{
parent::__construct();
}
public function get_output()
{
echo "Here is your output";
}
}
$obj = new Second();
$obj->call_child();
?>
There is a typo when calling the get_output method. However you should define the First class and get_output method as abstract in order to be extended/implemented by the other classes:
abstract class First
{
public function __construct()
{
echo "First class is initiated.";
}
abstract public function get_output();
public function call_child()
{
$this->get_output();
}
}
class Second extends First
{
public function get_output()
{
echo "Here is your output";
}
}
$obj = new Second();
$obj->call_child();
Note: the Second class may not define the constructor if it only calls the parent constructor.

Call Child Method in Main Class

I wanna call a Child Method in Main Class without declare new child();
class main {
function __construct() {
}
public function test() {
}
}
class child extends main {
function __construct() {
}
function childmethod() {
return "test";
}
}
$main = new main();
$main->childmethod();
best wishes
Make class as abstract and declare the method you want to call from child class as abstract.
abstract class main {
function __construct() {
}
public function test() {
return "test";
}
abstract function childmethod();
}
class child extends main {
function __construct() {
}
function childmethod() {
return "childmethod";
}
}
$main = new child();
echo $main->childmethod();// echoes childmethod
echo $main->test();// echoes test
If I correctly understand what you're asking for, you cannot do it and it's a mistake in the project idea.
If you need to call a child method in the parent, it means the method should go in the parent.
There's no way to call a method of an object without instantiate the object, unless you create a static method

Strategy pattern not working: What am I doing wrong?

I am a very beginning in PHP and Design Patterns. I have been studying the amazing Head First Design patterns. So, I have been trying to translate the original to PHP. However, I must be doing things very wrong, because it is not working. Can you help me?
<?php
interface FlyBehavior{
public function fly();
}
class FlyWithWings implements FlyBehavior{
public function fly(){
echo "I am flying!";
}
}
class FlyNoWay implements FlyBehavior{
public function fly(){
echo "I cannot fly!";
}
}
interface QuackBehavior{
public function quack();
}
class Quack implements QuackBehavior{
public function quack(){
echo "Quack";
}
}
class MuteQuack implements QuackBehavior{
public function quack(){
echo "Silence";
}
}
class Squeak implements QuackBehavior{
public function quack(){
echo "Squeak";
}
}
abstract class Duck{
protected $quackBehavior;
protected $flyBehavior;
public function performQuack(){
$this->$quackBehavior->quack();
}
public function performFly(){
$this->$flyBehavior->fly();
}
abstract public function display();
public function swim(){
echo "All ducks float, even decoy";
}
}
class MallardDuck extends Duck{
public function __construct(){
$this->quackBehavior=new Quack();
$this->flyBehavior=new FlyWithWings();
}
public function display(){
echo "I am real Mallard duck!";
}
}
$mallard=new MallardDuck();
$mallard->performQuack();
$mallard->performFly();
?>
When I run I get the error "Undefined variable: quackBehavior" in line "$this->$quackBehavior->quack();" inside "abstract class Duck".
The error says it all
public function performQuack(){
$this->$quackBehavior->quack();
}
public function performFly(){
$this->$flyBehavior->fly();
}
should be
public function performQuack(){
$this->quackBehavior->quack();
}
public function performFly(){
$this->flyBehavior->fly();
}
$this->quackBehaviory refers to a property of your class.
$quackBehaviory means that it's a function scoped variable.
So to access the class property you need to use $this->quackBehaviory
Remove the $ from $this->$quackBehavior->quack(); and $this->$flyBehavior->fly(); i.e. infront of $quackBehavior and $flyBehavior
Should be like this....
$this->quackBehavior->quack(); //<--- Under your performQuack()
and
$this->flyBehavior->fly(); //<--- Under your performFly()

Inheriting instance in PHP

I try to call getTest() function inside Child class.
First I initialize instance of Ext class so I assume main property $ext should now contain it. But Child class does not inherit it and obtained error message is:
Call to a member function getTest() on a non-object
Where is the issue?
<?php
$A = new Main;
class Main
{
protected $ext = null;
function __construct()
{
$this->ext= new Ext();
new Child;
}
}
class Child extends Main
{
function __construct()
{
echo $this->ext->getTest();
}
}
class Ext extends Main
{
public function getTest()
{
return "cool";
}
}
?>
I know that to solve it other way I can use:
class Child
{
private $Main;
function __construct( &$Main ) { ... }
}
but I would like to understand why that does not work..
At the moment on construct the object the atribute don't have a value yet.
You need call to the parent constructor before use the attribute
function __construct()
{
parent::__construct();
echo $this->ext->getTest();
}

Categories