Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion over when to instantiate parent from child class in php

Tags:

oop

php

I'm creating a website but, in order to further my coding skills, I'm trying to do it utilising the power of OOP.

I'm using classes to validate form input so thought I'd have a 'parent' validation class and then child classes for each form that gets submitted (i.e. login class, registration class etc) that would take care of putting the right values into the database etc.

The code I've seen has the parent being constructed from the child's constructor. However, I've not done that but my class seems to work anyway?

Could someone explain to me why we call the parent constructor from the child? Also, is my code only working because I have 'public' functions (methods) in my parent? (is this potentially an issue)?

My code (abridged version for clarity) is below:

class Validation_Class
{
 public function __construct()
{
 // constructor not needed
 }

 public function is_genuine_email_address($email) {
     // code to validate email are genuine here...
     }

 }

My child class looks like...

class Login_Class extends Validation_Class
{

public function __construct()
{
    // I don't call parent::__construct() from here
    // should I be doing?
    // I can still access parent methods with $this->is_genuine_email_address
    }

 }

All my functions (methods) in my Validation_Class are 'public' and when I instantiate my child class I can call any of the Validation Class methods with:

$className = "Login_Class";
$thisClass = new $className();
like image 533
John T Avatar asked Feb 05 '14 21:02

John T


1 Answers

It is not nessecary to call the parent constructor

class Parent {
  //maybe just holding some constants
  public $database = 'mydatabase';
}

class Child extends Parent {
   public function myFunction() {
     if ($this->database == 'myDatabase') {
       // you can access the parents data without calling a constructor
     }
  }
}

Is good. But if you want to benefit from something the parent has to do itself in order to work properly, a call to the parent __construct could be needed - like

class Parent {
  public $database = null;
  public function __construct() {
    // example -> login to database
  }
}

class Child extends Parent {
  public function __construct() {
    parent::__construct();
    // .. further code
  }

  public function myFunction() {
     // do something, like executing a query
     $this->database->executeQuery($SQL);
  }
}

In PHP "OOP", which is not real OOP like you see in other languages, constructors are just shorthands for instantiating the resulting object. It would be a hell if we over and over should call

$object = new MyClass();
$object->instantiate()

so calling __construct or new ClassName() is easier. But it is not absolutely needed for the class-successors to work properly, that they call constructors up in the class-hierarchy. Unless, of course, some certain initialization is needed in one of the class parents to let the successors work properly.

like image 134
davidkonrad Avatar answered Oct 06 '22 00:10

davidkonrad