Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Where to use "__construct" magic function

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 103
    Comment on it

    __construct

    The constructor is a magic method, which is immidately initated after declare the object of class and you can write the __construct method at any place in the class.
    Right here all of us have a case in point during which, develop a school Any along with a constructor function within this particular school:

    Here we take a example in which, create a class A and a constructor function inside this class:

    class A{
    
      public function __construct() {
        $this->created = time();
      }
    
    }
    

    Now we can create a class which inherits from the A class - a B! Without adding anything into the B class, we can declare it and have it inherit from A, like this:

    class B extends A {
    
    }
    

    $b = new B; echo $b->created;

    If we define a __construct method in the B class, then B objects will run that instead when they are instantiated. Since there isn't one, PHP looks to the parent class definition for information and uses that. So we can override, or not, in our new class - very handy.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: