开发者

php - is it possible for an object to reference parent objects methods?

Okay so I've got the hang of the basics writing classes and methods, and extending them.

I could easily write a massive class with all the methods I could possibly want or several classes that just keep extending each other in a chain. But things are starting to get difficult to manage.

I am wondering if I could do something like the following code so I can keep "modules" separated, and only fire them up when required.. I hope this makes some sort of sense as to what i'm hoping to achieve:

// user/member handling methods "module"
class db_user
{
    public function some_method()
    {
        // code that requires parent(?) objects get_something() method
    }
}

// post handling methods "module"
class db_post
{
    public function some_method()
    {
        // code that requires parent(?) objects update_something() method
    }
}

class db_connect()
{
    public $db_user;
    public $db_post;

    public function __construct()
    {
        // database connection stuff
    }
    public function __destruct()
    {
        // blow up
    }

    // if i want to work with user/member stuff
    public function set_db_user()
    {
        $this->db_user = new db_user();
    }

    // if i want to work with posts
    public function set_db_post()
    {
        $this->db_post = new db_post();
    }

    // generic db access methods here, queries/updates etc.
    public function get_something()
    {
        // code to fetch something
    }

    public function update_something()
    {
        // code to fetch something
    }
}

So i wou开发者_开发问答ld then create a new connection object:

$connection = new db_connect();

Need to work with users so..

$connection->set_db_user();
$connection->db_user->some_method();

And now i need to do something with posts so..

$connection->set_db_post();
$connection->db_post->some_method();
$connection->db_post->some_other_method();

I hope someone can help me out here, I've been searching for a few days but can't seem to find any info other than to basically keep it all in one class or create an endless chain of extensions - which isn't helpful because while I want everything to work through one "interface" I still want to keep the "modules" separate.

My apologies if this seems utterly ridiculous somehow - I am a novice after all..


Pass db_connection into the db_* classes:

class db_user
{
    protected $db;

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

    public function some_method()
    {
        // code that requires parent(?) objects update_something() method
        $this->db->update_something();
    }
}

Use:

$db = new db_connection();
$user = new db_user($db);
$user->some_method()

The db_connect should not have set_db_user, set_db_post, etc. It should handle connecting to the db and maybe some generic select/update/insert/delete methods


You can pass a reference to db_connect instance into the db_user/db_post constructors and store it into field $parent.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜