さて、私はクラスとメソッドを記述し、それらを拡張する基本のハングアップを持っています。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();
、私は数日のために検索してきたが、以外の任意の情報を見つけるように見えることはできません基本的にすべてを1つのクラスにまとめたり、無限の拡張機能をつくることができます。これは有用なことではありません。
私の謝罪これは何とか全くばかげている場合 - 私はあなたがDB_USER/db_postコンストラクタにインスタンスをdb_connectとフィールド$parent
にそれを保存するための参照を渡すことができます。..
私が正しく理解しているなら、それは "親"メソッドではなくr 「コンテナ」メソッドを使用します。オプションには 'db_connect'(ラッパーとして)のメソッドを介してアクセスを公開し、接続オブジェクトを渡すか、' db_user/db_post'オブジェクトを作成するときに 'db_connect'オブジェクトを渡します。ハッピーコーディング。 –