Hi, I'm currently a learning PHP Developer, and I was wondering, how do you use other classes inside of other classes.
What I mean is that throught the MyBB source code I've seen things like "$db->simple_select()" in classes like like "parser".
My question is how do you get PHP to do that, because when I try to create a new instance of a class ($db = new mysqli($host, $user, $pass, $name)), I get an error.
Thank you.
Those are called class operations. They're basically functions created within a class. Hence, you'd be instantiating the class via its name, then using that instance to call a function as used in your example $db->simple_select(); . The $db var is the instance of the class, and the simple_select would be one of the class operations or functions.
The subclasses if I remember correctly would be extending the superclass, and not actually within the superclass.
Thank you, but that was not the answer I was looking for.
Lets say I create a class called "Foo"
class Foo {
function getBar() {
$db->getSomeDBDATA();
$bar = $db->fetch_array();
return($bar);
}
}
Now, if I do that, I get an error, because that "$db->" doesn't really belong there, but I've seen it all the time in the MyBB source, so how do i get it to do that?
To call in other variables and objects, make sure you use this on the first line of a function:
global $db, $variables, $other_objects...;
(2009-09-26, 03:52 AM)Xadrieth Wrote: [ -> ]Thank you, but that was not the answer I was looking for.
Lets say I create a class called "Foo"
class Foo {
function getBar() {
$db->getSomeDBDATA();
$bar = $db->fetch_array();
return($bar);
}
}
Now, if I do that, I get an error, because that "$db->" doesn't really belong there, but I've seen it all the time in the MyBB source, so how do i get it to do that?
Ah. Then what Doug has posted is your solution. Thought you were having troubles calling an operation.
You can also pass a classes object by reference through a class constructor, and access it without using global.
Look at the following code, comments are outlined explaining what we're doing.
// This is our first class.
class first_class
{
// Class Constructor, noted by having the same name (in PHP4) or by using __construct (in PHP5)
function first_class ( )
{
return TRUE;
}
// An example method.
function first_class_method ( )
{
$string = 'I\'m from the first class, and I do nothing except return a string.';
return $string;
}
}
// This is our second class.
class second_class
{
var $first = NULL; // Set this, because the first class needs something to be associated with while it's inside the second class.
// Class Constructor
// We need to pass the first class in by reference, via the constructor. More on that later.
function second_class ( &$first )
{
// Associate the first class with the var we set earlier.
$this->first = &$first;
// Make sure it's an object!
if ( !is_object ( $first ) )
{
die ( 'We can\'t run without the first class!' );
}
}
// This is our second class method.
function second_class_method ( )
{
$string = '<p>'$this->first->first_class_method ( ) . '</p><p>I\'m from the Second class, and I do nothing except return both strings from both methods, compounded into one!</p>';
}
}
// Initialize the objects for our classes.
// Since we have constructors we need the classes to look like functions when initializing them, so we can pass through parameters if needed.
$first = new first_class ( ); // First class, pass nothing through, since it doesn't need any objects to run.
$second = new second_class ( $first ); // Second class, pass $first through, because it needs it to run.
// Now echo out the method from the second class, and you'll get both at the same time.
echo ( $second->second_class_method ( ) );
Yes, I'm bored, but there ya go. Play with it, and I hope you learn something from it.