6

I'm currently creating blog system, which I hope to turn into a full CMS in the future.

There are two classes/objects that would be useful to have global access to (the mysqli database connection and a custom class which checks whether a user is logged in).

I am looking for a way to do this without using global objects, and if possible, not passing the objects to each function every time they are called.

4

4 に答える 4

13

You could make the objects Static, then you have access to them anywhere. Example:

myClass::myFunction();

That will work anywhere in the script. You might want to read up on static classes however, and possibly using a Singleton class to create a regular class inside of a static object that can be used anywhere.

Expanded

I think what you are trying to do is very similar to what I do with my DB class.

class myClass
{
    static $class = false;
    static function get_connection()
    {
        if(self::$class == false)
        {
            self::$class = new myClass;
        }
        return self::$class;
    }
    // Then create regular class functions.
}

What happens is after you get the connection, using $object = myClass::get_connection(), you will be able to do anything function regularly.

$object = myClass::get_connection();
$object->runClass();

Expanded

Once you do that static declarations, you just have to call get_connection and assign the return value to a variable. Then the rest of the functions can have the same behavior as a class you called with $class = new myClass (because that is what we did). All you are doing is storing the class variable inside a static class.

class myClass
{
    static $class = false;
    static function get_connection()
    {
        if(self::$class == false)
        {
            self::$class = new myClass;
        }
        return self::$class;
    }
    // Then create regular class functions.
    public function is_logged_in()
    {
        // This will work
        $this->test = "Hi";
        echo $this->test;
    }
}

$object = myClass::get_connection();
$object->is_logged_in();
于 2009-07-18T17:29:50.997 に答える
8

You could pass the currently global objects into the constructor.

<?php
  class Foo {
    protected $m_db;
    function __construct($a_db) {
      $this->m_db = $a_db;
    }
  }
?>
于 2009-07-18T17:32:42.333 に答える
3

最近、自社の CMS の 2 番目のバージョンに備えて、フレームワークを刷新しました。通常のオブジェクトに置き換えるために、静的にした大量のものを元に戻しました。そうすることで、以前はコアファイルを調べてハッキングすることに頼っていた非常に大きな柔軟性を生み出しました。唯一の代替手段がグローバル関数である場合にのみ、静的構造を使用するようになりました。グローバル関数は、低レベルのコア機能にのみ関連しています。

私の言いたいことを示すために、bootstrap.php ファイルの数行を示します (すべての要求はそのファイルを介して送信されますが、すべてのファイルの先頭に含めることで同じ結果を得ることができます)。これは、おそらくあなたの状況で使用するもののかなり重いバージョンですが、このアイデアが役立つことを願っています. (これはすべてわずかに変更されています。)

 //bootstrap.php

...

// CONSTRUCT APPLICATION

{       
    $Database = new Databases\Mysql(
        Constant::get('DATABASE_HOST'),
        Constant::get('DATABASE_USER'),
        Constant::get('DATABASE_PASSWORD'),
        Constant::get('DATABASE_SCHEMA')
    );

    $Registry     = new Collections\Registry;
    $Loader       = new Loaders\Base;
    $Debugger     = new Debuggers\Dummy; // Debuggers\Console to log debugging info to JavaScript console

    $Application  = new Applications\Base($Database, $Registry, $Loader, $Debugger);
}

...

ご覧のとおり、アプリケーション オブジェクトを作成するためのあらゆる種類のオプションがあります。これらのオプションをコンストラクターで引数として他のオブジェクトに提供し、これらの "グローバル" な必需品にアクセスできるようにします。

データベース オブジェクトは一目瞭然です。レジストリ オブジェクトは、アプリケーション内の別の場所にアクセスする可能性のあるオブジェクトのコンテナーとして機能します。ローダーは、テンプレート ファイルなどの他のリソースをロードするためのユーティリティとして機能します。また、デバッガーはデバッグ出力を処理するために存在します。

たとえば、インスタンス化するデータベース クラスを変更すると、SQLite データベースに接続できます。(前述のように) デバッガーのクラスを変更すると、すべてのデバッグ情報が JavaScript コンソールに記録されるようになります。

さて、問題に戻ります。他のオブジェクトにこれらすべてへのアクセスをどのように許可しますか? コンストラクターに引数として渡すだけです。

// still bootstrap.php

...

// DISPATCH APPLICATION

{
    $Router = new Routers\Http($Application);
    $Router->routeUri($_SERVER['REQUEST_URI']); 
}

...

それだけでなく、Router (またはそれを使用して作成するオブジェクト) もより柔軟です。これで、アプリケーション オブジェクトを別の方法でインスタンス化でき、それに応じて Router の動作が異なります。

于 2013-08-10T04:29:56.030 に答える
1

Well, if you already have some object by which you refer to the blog system, you can compose these objects into that, so that they're $blog->db() and $blog->auth() or whatever.

于 2009-07-18T17:30:55.033 に答える