Results 1 to 6 of 6
  1. #1

    What is a constructor for OOP PHP?

    I searched all over the internet and it is explained in a way that I will never understand. Can anyone explain in plain english why a constructor is necessary in object oriented programming or at least what makes it different than a normal function used inside a class?

  2. #2
    Join Date
    Feb 2002
    New York, NY
    A constructor is code that runs automatically when you instantiate an object. It's basically a function within the class that has the same name as the class. Sometimes they accept arguments, like below:

    $moose = new Moose("Bullwinkle");
    Scott Burns, President
    BQ Internet Corporation
    Remote Rsync and FTP backup solutions
    *** ***

  3. #3
    To expand on bqinternet's reply, a PHP object constructor isn't necessary, but just a feature of using objects. Compare defining an object to defining a function:

    function moose($name)
    // do stuff with $name

    class Moose
    public function __construct($name)
    // do what you want with the name

    Every time you "$moose = new Moose('Bullwinkle');", it runs that __construct() function and uses what you supply to Moose() just like if you defined it as a normal function.

    There also exist things called "destructors" that run when the object is destroyed (used mostly when the script stops running).

  4. #4
    Thanks, I have one last question. I am reading a lot of OOP tutorials out there and all of them are only a page long. Is there not much to OOP, or are only the basics covered? Does this tutorial cover it all?

  5. #5
    The constructor is having some code within the braces that code automatically executed.Their is no need to call the constructor explicitly.

  6. #6
    Join Date
    Apr 2011
    Charlotte, NC
    OOP is a programming concept that spans many different programming languages. I actually learned just about everything I know about OOP when I learned Java.

    The basics of OOP are not complicated, but there are millions of ways to actually implement it into your code.

    Once you have a grasp of the basics, you will want to pick up a book(or find an online tutorial) on 'OOP Design Patterns'. The real power in OOP is how you get your class objects to interact with each other.
    Adam Hahn
    Software Engineer / System Administrator / Database Engineer

Similar Threads

  1. PHP Templates / OOP
    By BoggeRs in forum Programming Discussion
    Replies: 5
    Last Post: 07-20-2008, 03:45 AM
  2. PHP Debate: OOP vs Not OOP
    By Floridaserver in forum Programming Discussion
    Replies: 34
    Last Post: 12-08-2007, 04:44 PM
  3. PHP OOP standards and rules?
    By OddLaW in forum Programming Discussion
    Replies: 5
    Last Post: 10-02-2005, 08:04 AM
  4. need some help with a constructor(PHP)
    By w3needs in forum Programming Discussion
    Replies: 4
    Last Post: 04-08-2005, 12:32 AM
  5. PHP - OOP or not?
    By w3bdesign in forum Programming Discussion
    Replies: 11
    Last Post: 10-05-2004, 10:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts