Page 1 of 1
Question about how classes work
Posted: Sat Jan 20, 2007 4:15 pm
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?
Posted: Sat Jan 20, 2007 4:24 pm
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
Posted: Sat Jan 20, 2007 4:35 pm
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');
Posted: Sat Jan 20, 2007 4:41 pm
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
You don't need quotes around it, the fallowing works the same:
Posted: Sat Jan 20, 2007 4:42 pm
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?
Posted: Sat Jan 20, 2007 4:44 pm
by Z3RO21
Lesson of this post is
http://www.php.net is our friend

Posted: Sat Jan 20, 2007 4:50 pm
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.
Posted: Sat Jan 20, 2007 6:44 pm
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.