php - 对象是否可以引用父对象方法?

发布于 2024-11-19 09:12:15 字数 1795 浏览 1 评论 0原文

好的,我已经掌握了编写类和方法以及扩展它们的基础知识。

我可以轻松地编写一个包含我可能想要的所有方法的大型类,或者编写几个在链中不断相互扩展的类。但事情开始变得难以管理。

我想知道我是否可以执行类似以下代码的操作,以便我可以将“模块”分开,并且仅在需要时才启动它们。我希望这对我希望实现的目标有某种意义:

// 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
    }
}

所以我然后会创建一个新的连接对象:

$connection = new db_connect();

需要与用户合作,所以..

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

现在我需要对帖子做一些事情,所以..

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

我希望有人可以在这里帮助我,我已经搜索了几天但不能似乎找到了除了基本上将其全部保留在一个类中之外的任何信息,或者创建一个无尽的扩展链 - 这没有帮助,因为虽然我希望一切都通过一个“接口”工作,但我仍然希望将“模块”分开。

如果这看起来完全荒谬,我深表歉意 - 毕竟我是新手..

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 would 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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小忆控 2024-11-26 09:12:15

db_connection 传递到 db_* 类中:

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();
    }
}

使用:

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

db_connect 不应具有 set_db_userset_db_post< /code> 等。它应该处理与数据库的连接,也许还有一些通用的选择/更新/插入/删除方法

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

从来不烧饼 2024-11-26 09:12:15

您可以将对 db_connect 实例的引用传递到 db_user/db_post 构造函数中,并将其存储到字段 $parent 中。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文