MyBB Community Forums

Full Version: Calling a class inside another - PHP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,
Just a PHP question ^_^.

I am currently doing a bit of experimenting with OOP and was wondering how you would go about calling a class inside another.

For example I have a user class but that needs to access functions in the DB class. Also I have an appointment class that needs to access functions in the user class?

Is there a good way to go about this or should I be doing this at all? Toungue

Thanks for all replies in advance Smile
Do you mean extends? I think you want to do what control_object does but forgive me if not, I'm also starting with classes Smile
I wouldn't extend a class in such a case, personally. You should perform a class extension when you are making specific use of a class. For example, if you have a generic users class and you create a more specific users class for your application, you would extend the generic users class. In your case, you should create an instance of the db class in your users class, and use that.

All the best,
Imad Jomaa.
<?php 

class DB {

    function some_method()
    {

    }

}

?>

<?php

class Users {

    function __construct()
    {
         require_once PATH_TO_DB_CLASS
         $this->$db = new DB
    }

     function something()
    {
        $this->db->some_method();
    }



I personally like this approach...well to be honest I assign all the classes to a "superglobal" $this and then use that everywhere, but thats just me Wink.

You could also use a $db = new DB but my way you can use $this->db in the whole class.
Ok thanks guys, I'll experiment some more and see what works best for me Toungue
This is the only way you should use classes within classes:

<?php
class User
{
	protected $db;
	
	public function setDatabase( $db )
	{
		$this->db = $db;
	}
}

$db = new DB;
$user = new User;
$user->setDatabase($db);

This way, you can use the class in another project without modifying it, or dynamically switch to another database object before running a given method. If you have classes that rely on other classes having a certain name or being in a certain location, you end up with spaghetti code because suddenly everything relies on everything else existing.

@Jitendra: If everything has access to everything, it completely defeats the purpose of OOP. Your database object shouldn't be available outside models, for example. Forces you and others to keep your code clean.
(2012-08-02, 08:26 AM)CAwesome Wrote: [ -> ]This is the only way you should use classes within classes:

<?php
class User
{
	protected $db;
	
	public function setDatabase( $db )
	{
		$this->db = $db;
	}
}

$db = new DB;
$user = new User;
$user->setDatabase($db);

...

That really depends on what you want to do. That's not how you should _only_ load classes within classes. You can load a class from within another if necessary. You don't need to pass the object just for the sake of it. If you want a higher abstraction level you don't even want to pass an object - unless required.