php - 对象是否可以引用父对象方法?
好的,我已经掌握了编写类和方法以及扩展它们的基础知识。
我可以轻松地编写一个包含我可能想要的所有方法的大型类,或者编写几个在链中不断相互扩展的类。但事情开始变得难以管理。
我想知道我是否可以执行类似以下代码的操作,以便我可以将“模块”分开,并且仅在需要时才启动它们。我希望这对我希望实现的目标有某种意义:
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
db_connection
传递到db_*
类中:使用:
db_connect
不应具有set_db_user
、set_db_post< /code> 等。它应该处理与数据库的连接,也许还有一些通用的选择/更新/插入/删除方法
Pass
db_connection
into thedb_*
classes:Use:
The
db_connect
should not haveset_db_user
,set_db_post
, etc. It should handle connecting to the db and maybe some generic select/update/insert/delete methods您可以将对 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
.