CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #1
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    PHP: __construct() question

    Hello php gurus

    If a class has a method named after the class, is the function constructor?
    If I have a base class as
    PHP Code:
    // Inheritance test
    class Animal
    {

        function 
    __construct($ref)
        {
            
    $this->animalName $ref;
        }

        function 
    eat()
        {
            
    printf("%s is eating<br />"$this->animalName);
        }

        protected 
    $animalName;
        

    and I'd like to call the base's constructor in the derived
    PHP Code:
    //
    class Panda extends Animal
    {
        function 
    __construct($ref)
        {
            
    $this->Animal($ref); //works only if Animal($ref) is defined in the base
        
    }

        function 
    eat()
        {
            
    printf("Big %s is eating<br />"$this->animalName);
            return 
    $this;
        }

        function 
    yawn()
        {
            
    printf("Big %s is yawning<br />"$this->animalName);
        }

    What is the difference between __construct() and the function with the same class name?

    Thanks for the help.
    Last edited by potatoCode; July 18th, 2009 at 02:19 AM. Reason: changed to the proper php tag

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured