Question about how classes work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jhorra
Forum Newbie
Posts: 24
Joined: Mon Aug 18, 2003 1:00 am

Question about how classes work

Post by Jhorra »

I have a class that extends a second class. The base class has a constructor function, but the base class doesn't. When I call the class, do I need to provide the variable the base class requires for the constructor, or does it ignore the base class constructor?

Extending that question, if both classes have constructors, and they both require a variable, how do I pass both variables?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Jhorra wrote:The base class has a constructor function, but the base class doesn't. When I call the class, do I need to provide the variable the base class requires for the constructor, or does it ignore the base class constructor?
I think you meant the parent and the base class. Your parent's classes constructor will not be called unless you specifically call it.

Code: Select all

class Base extends Parent
{
   public function __construct($param)
   {
      parent::__construct();
   }
}
Jhorra wrote:Extending that question, if both classes have constructors, and they both require a variable, how do I pass both variables?
Consider extending joining the classes together. They have access to each other's methods and variables.

Code: Select all

class Base extends Parent
{
   protected $foobar;

   public function __construct($param)
   {
      $this->foobar = $param;
   }
}

class Parent
{
   function foobar() 
   {
      echo $this->foobar;
   }
}

$foo = new Base('bleh bloo');
$foo->foobar(); //outputs bleh bloo
Last edited by John Cartwright on Sat Jan 20, 2007 4:36 pm, edited 1 time in total.
Jhorra
Forum Newbie
Posts: 24
Joined: Mon Aug 18, 2003 1:00 am

Post by Jhorra »

So if I have:

Code: Select all

class producer
{
     function producer($user_id)
     {
           //Executes code
     }
}

class track extends producer
{
     //code
}
If I were to use the above, to call the producer function I would use this?

Code: Select all

$track = new track();
$track->producer('1');
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

Yup, have you looked at the manual yet?

PHP4 - http://us3.php.net/manual/en/language.oop.php
PHP5 - http://us3.php.net/manual/en/language.oop5.php

Code: Select all

$track->producer('1');
You don't need quotes around it, the fallowing works the same:

Code: Select all

$track->producer(1);
Jhorra
Forum Newbie
Posts: 24
Joined: Mon Aug 18, 2003 1:00 am

Post by Jhorra »

I didn't know those were there, very helpful. Thanks.

Quick question though, when you use :: does that mean you are calling it like a static function?
Last edited by Jhorra on Sat Jan 20, 2007 4:44 pm, edited 1 time in total.
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

Lesson of this post is http://www.php.net is our friend :wink:
Jhorra
Forum Newbie
Posts: 24
Joined: Mon Aug 18, 2003 1:00 am

Post by Jhorra »

I knew about the function reference, but I didn't know they had tutorials and overviews of bigger concepts like that in there.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

There are plenty of articles and tutorials on the web that cover OOP in PHP, and you'll probably get the information you need a lot faster anyway.

The scope resolution operator ( :: ) does call a method statically.
Post Reply