Question about how classes work
Moderator: General Moderators
Question about how classes work
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?
Extending that question, if both classes have constructors, and they both require a variable, how do I pass both variables?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I think you meant the parent and the base class. Your parent's classes constructor will not be called unless you specifically call it.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?
Code: Select all
class Base extends Parent
{
public function __construct($param)
{
parent::__construct();
}
}Consider extending joining the classes together. They have access to each other's methods and variables.Jhorra wrote:Extending that question, if both classes have constructors, and they both require a variable, how do I pass both 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.
So if I have:
If I were to use the above, to call the producer function I would use this?
Code: Select all
class producer
{
function producer($user_id)
{
//Executes code
}
}
class track extends producer
{
//code
}Code: Select all
$track = new track();
$track->producer('1');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
You don't need quotes around it, the fallowing works the same:
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');Code: Select all
$track->producer(1);Lesson of this post is http://www.php.net is our friend 
- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
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.
The scope resolution operator ( :: ) does call a method statically.