php calling a parent constructor - php

class Dad {
private $par1;
function __construct(int $par)
{
$this->par1 = $par;
}
}
class Child extends Dad {
private $par2;
function __construct(int $par1, int $par2)
{
$this->par2 = $par2;
parent::__construct($par1);
}
}
Having this code I have a question about calling parent::__construct($par1) in the child class. Should we call it before initializing variables or after initializing them like in my example or it does not matter ?

Related

A counter variable in a php abstract class

If you have an abstract class, is there a way to keep a counter inside the abstract class to count how many times it's used?
Because if I do this:
abstract class abstractClassName {
private $counter = 0;
public function __construct() {
$this->counter++;
}
public function outputCounter() {
echo $this->counter;
}
}
class someExtension extends abstractClassName {
public function __construct() {
parent::__construct();
}
}
class someExtensionTwo extends abstractClassName {
public function __construct() {
parent::__construct();
}
}
and then
$class = new someExtension;
$class->outputCounter();
$class2 = new someExtensionTwo;
$class2->outputCounter();
I get 1 twice, yet I was expecting to get 1 and then 2 on the last call, and I'm getting confused over how it's all meant to work.
Edit: changed code to reflect the real code more :)
If you are wishing to keep track of how many time the class AbstractClassName has been instantiated, you will need to make use of static variables -- so that the value of $this->counter is persistent, for example:
abstract class abstractClassName
{
private static $counter = 0;
public function __construct() {
self::$counter++;
}
public function outputCounter() {
echo self::$counter;
}
}
class someExtension extends abstractClassName
{
public function __construct() {
parent::__construct();
}
}
class someOtherExtension extends abstractClassName
{
public function __construct() {
parent::__construct();
}
}
$class = new someExtension;
$class->outputCounter();
$class2 = new someOtherExtension;
$class2->outputCounter();
Which would result in an output of: 1 & 2.

PHP OOP inheritance issue

I have an inheritance issue. Consider I have a parent class A and a derived class B from A and a derived Class C from A again.
Let's say, class B & C both override method seed() from class A. Also clases A & B & C all implement a static function seedAll().
My question is: How do I call seedAll for B & C from the seedAll function of A without explicitly specifying the class B & C, so that in the future, should I have a class D similar to class B & C, I need not modify the seedAll function of class A to call the seedAll functions of classes B, C & D.
abstract class A
{
public static function seedAll() {
//How do I call seedAll of all the derived classes of Seeder?
}
abstract protected function seed();
// // Common method
// public function printOut() {
// print $this->getValue() . "\n";
// }
}
class B extends A
{
private $name;
function __construct($name) {
$this->name = $name;
}
public static function seedAll() {
$seeder1 = new B("name1");
$seeder1 = new B("name2");
}
function seed() {
echo $this->name;
}
}
class C extends A
{
private $name;
function __construct($name) {
$this->name = $name;
}
public static function seedAll() {
$seeder1 = new C("name1");
$seeder1 = new C("name2");
}
function seed() {
echo $this->name;
}
}
See how to obtain all subclasses of a class in php for how to get all the subclasses. You can then loop over them and call their seedAll() methods.
abstract class A
{
public static function seedAll() {
$subclasses = getSubclassesOf("A");
foreach ($subclasses as $class) {
$class::seedAll();
}
}
abstract protected function seed();
// // Common method
// public function printOut() {
// print $this->getValue() . "\n";
// }
}
getSubclassesOf function:
function getSubclassesOf($parent) {
$result = array();
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, $parent))
$result[] = $class;
}
return $result;
}

Cannot re-assign $this: Use reference of an existing object A into another object B which class extends the one of object A

In PHP (Symfony 3);
I want to reference an existing object A in another object B which class extends the one of object A, like this:
class A {
private $property1;
private $property2;
public function __construct($p1,$p2){
$this->property1 = $p1;
$this->property2 = $p2;
}
}
class B extends A {
private $property3;
public function __construct($objectA,$p3){
$this = $objectA;
$this->property3 = $p3;
}
}
$a = new A('p1','p2');
$b = new B($a,'p3');
This does not work and throw the following error at the statement $this = $objectA:
Compile Error: Cannot re-assign $this
Which are documented and explain there and there. I am looking for a workaround.
You must call parent constructor and also make property1 and property2 visible in class B
<?php
class A {
private $property1;
private $property2;
public function __construct($p1,$p2){
$this->property1 = $p1;
$this->property2 = $p2;
}
public function getProperty1()
{
return $this->property1;
}
public function getProperty2()
{
return $this->property2;
}
}
class B extends A {
private $property3;
public function __construct($objectA,$p3){
parent::__construct($objectA->getProperty1(), $objectA->getProperty2());
$this->property3 = $p3;
}
}
$a = new A('p1','p2');
$b = new B($a,'p3');
See it live here: http://sandbox.onlinephpfunctions.com/code/705bf1827da2bdf10f8d961ee1cb6fbdd88bc663
As an alternative, you could use __call magic method to forward all cals to class A:
<?php
class A {
private $property1;
private $property2;
public function __construct($p1,$p2){
$this->property1 = $p1;
$this->property2 = $p2;
}
}
class B extends A {
private $property3;
private $a;
public function __construct($objectA,$p3){
$this->a = $objectA;
$this->property3 = $p3;
}
public function __call($name, $arguments)
{
return call_user_func_array(array($this->a, $name), $arguments);
}
}
$a = new A('p1','p2');
$b = new B($a,'p3');
Based on how to clone object to child class in php
Using get_object_vars on the parent object, you can get an array of properties keys and values. You can then loop through them and assign them to the child object:
<?php
class A {
protected $property1;
protected $property2;
public function __construct($p1,$p2){
$this->property1 = $p1;
$this->property2 = $p2;
}
}
class B extends A {
private $property3;
public function __construct($objectA,$p3){
//$this = $objectA;
$objValues = get_object_vars($objectA); // return array of object values
foreach($objValues AS $key=>$value)
{
$this->$key = $value;
}
$this->property3 = $p3;
echo $this->property1;
}
}
$a = new A('p1','p2');
$b = new B($a,'p3');
This does not work with private properties, they need to be at least of protected level.
I ended up managing it like that:
class B extends A{
public function __construct($objectA){
foreach($this as $k => $v){
if(isset($objectA->{$k})){
$this->{$k} = &$objectA->{$k};
}
}
}
}
Compare to #Antony answer, notice it has & in front of $objectA->{$k}: $this->{$k} = &$objectA->{$k};. As I understood it, with &, any change on $objectB of properties belonging to the extended class A applies to $objectA.
I am aware it is not perfect and quite hacky but it does the job I need. Thanks for the input given by everybody.

How do you override self?

I want to build class that implement a lot of methods based on it's id,
so I want to have one class parent that implement the methods!
and when i want to use this class I will extend it and just override the id variable :
class parent
{
$id = "parent";
private __construct()
{
}
public static function create_instance()
{
$instance = new self();
return $instance;
}
public static function print_id()
{
echo $this->id;
}
}
class child extend parent
{
$id = "child";
}
$instance = child::create_instance();
$instance->print_id();
the result will be "parent", but I want the result to be child ?
How to do that ?
EDIT : I also tried this and got parent instead of child:
class parent1 {
private $id = "parent";
public function __construct() {
}
public static function create_instance() {
$instance = new static ();
return $instance;
}
public function print_id() {
echo $this->id;
}
}
class child extends parent1 {
private $id = "child";
}
$instance = child::create_instance ();
$instance->print_id ();
The problem is that the visibility of $id is private whereas it should be protected because print_id() is only defined on the parent; as such it can only reach its own $id.
class parent1 {
protected $id = "parent";
// ...
}
class child extends parent1 {
protected $id = "child";
}
The alternative is, of course, to override print_id() in the child class.
Currently when you call create_instance method on child class as a result instance of parent class is created not child class as you expect.
Use late static binding in parent class "create_instance" method:
public static function create_instance()
{
$instance = new static();
return $instance;
}
More details http://php.net/manual/en/language.oop5.late-static-bindings.php

Accessing an object declared in parent class in PHP

Edit: Original example removed as it was complex.
The codes provided below doesn't work. I am trying to access the methods defined in a class which is declared in the parent class.
Here is a sample code. Its not working and I'd like to know why
<?php
function & get_instance()
{
return Main::get_instance();
}
class Db{
function select($var)
{
echo $var;
}
}
class Main
{
public $db ;
public $process ;
private static $instance;
function __construct()
{
self::$instance = &$this;
$this->db = new Db ;
$this->process = Process;
}
public static function & get_instance()
{
return self::$instance;
}
}
class Process{
private $main ;
function __construct()
{
$this->main = get_instance() ;
}
function processPayment()
{
$this->main->db->select("hello");
}
}
$main = new Main ;
$main->process->processPayment();
To access members of a parent class, you will have to declare those members protected or public.
For example:
public var $db;
protected var $orders;

Categories