Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,581 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,929 people online right now. Registration is fast and FREE... Join Now!




Please Explain This Code

 
Reply to this topicStart new topic

Please Explain This Code

usamamuneeb
29 Sep, 2008 - 08:11 PM
Post #1

D.I.C Head
**

Joined: 4 Sep, 2008
Posts: 67


My Contributions
CODE
<?php
// PHP 5

// class definition
class Bear {
    // define properties
    public $name;
    public $weight;
    public $age;
    public $sex;
    public $colour;

    // constructor
    public function __construct() {
        $this->age = 0;
        $this->weight = 100;
    }

    // define methods
    public function eat($units) {
        echo $this->name." is eating ".$units." units of food... ";
        $this->weight += $units;
    }

    public function run() {
        echo $this->name." is running... ";
    }

    public function kill() {
        echo $this->name." is killing prey... ";
    }

    public function sleep() {
        echo $this->name." is sleeping... ";
    }
}

// extended class definition
class PolarBear extends Bear {

    // constructor
    public function __construct() {
        parent::__construct();
        $this->colour = "white";
        $this->weight = 600;
    }

    // define methods
    public function swim() {
        echo $this->name." is swimming... ";
    }
}

?>


Please explain the line 43:

CODE
parent::__construct();


The tutorial is from: http://devzone.zend.com/node/view/id/638
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Please Explain This Code
29 Sep, 2008 - 08:46 PM
Post #2

D.I.C Regular
***

Joined: 8 Aug, 2008
Posts: 383



Thanked: 23 times
My Contributions
Somebody needs to do their own homework.
rolleyes.gif

User is offlineProfile CardPM
+Quote Post

DilutedImage
RE: Please Explain This Code
29 Sep, 2008 - 09:15 PM
Post #3

D.I.C Addict
Group Icon

Joined: 20 Nov, 2006
Posts: 642



Thanked: 6 times
Dream Kudos: 25
My Contributions
Here's a good place to start:
Scope Resolution Operator


User is offlineProfile CardPM
+Quote Post

Moonbat
RE: Please Explain This Code
30 Sep, 2008 - 02:55 AM
Post #4

D.I.C Regular
Group Icon

Joined: 30 Jun, 2008
Posts: 391



Thanked: 22 times
Dream Kudos: 600
My Contributions
Even if this is a homework assignment, it's so easy I'll go ahead and explain it.

parent::__construct() runs the steps from the parent function Bear's __construct() method. The child class PolarBear adds two additional steps of it's own to it's own __construct() method, in addition to running the parent's __construct() method.
User is offlineProfile CardPM
+Quote Post

usamamuneeb
RE: Please Explain This Code
30 Sep, 2008 - 04:00 AM
Post #5

D.I.C Head
**

Joined: 4 Sep, 2008
Posts: 67


My Contributions
Well, thanks to Moonbat, but don't you all think the zend website lacks explanation? And I wonder who can go through the manuals presented on official sites. Maybe I should switch the tutorial, this is my fourth post asking for explanation.

And I wonder if anyone can read READMEs. I can't.
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Please Explain This Code
30 Sep, 2008 - 05:06 AM
Post #6

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 580



Thanked: 59 times
Dream Kudos: 50
My Contributions
The explanation is right in the tutorial:
QUOTE
Note how the parent class constructor has been called in the PolarBear() child class constructor - it's a good idea to do this so that all necessary initialization of the parent class is carried out when a child class is instantiated. Child-specific initialization can then be done in the child class constructor. Only if a child class does not have a constructor, is the parent class constructor automatically called.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 12:30AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month