php class - 制作一个小包装器

时间:2015-08-14 09:40:14

标签: php class instance wrapper

我目前正在为类似帖子的系统制作类插件。

但是,我想制作一种类包装器,将它用作一个简单的函数。但我需要在我的课程中注册一些脚本和常量然后运行该类。

使用当前的包装器,我重新执行了类的_contruct函数。我想初始化类然后当我调用tiny包装器时它不会执行contrucct实例并且只获得正确的类函数。

我该怎么做?这有意义吗?

这是我的示例代码:

if(!class_exists('Post_Like')) {

    class Post_Like {

        static private $instance = null;

        static public function getInstance() {
            if(self::$instance == null) {
                self::$instance = new self;
            }
            return self::$instance;
        }

        public function __construct() {
            $this->define_constants();
            // Load public/admin CSS and JS.
            add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
            add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
            // register actions for ajax
            add_action( 'wp_ajax_nopriv_post_like', array($this, 'post_like') );
            add_action( 'wp_ajax_post_like', array($this, 'post_like') );
        }

        public function define_constants() {
            define('POST_LIKE_PATH', trailingslashit(str_replace('\\', '/', dirname(__FILE__))));
            define('POST_LIKE_URL', site_url(str_replace(trailingslashit(str_replace('\\', '/',ABSPATH)), '', POST_LIKE_PATH)));
        }


        public function enqueue_scripts() { 
            // enqueue js scripts
        }

        public function post_like() {
           // do some stuff 
        }


        public function post_already_like() {
           // do some stuff         
        }

        // I want to wrap this function to use it anywhere easily
        public function get_post_like() {
           // do some stuff
        }

    }

}

new Post_Like();

if(!function_exists('Post_Like')) {
    /**
    * Tiny wrapper function
    */
    function get_post_like() {
        $Post_Like = Post_Like::getInstance();
        return $Post_Like->get_post_like(); // it execute _contruct, I want to avoid that
    }

}

1 个答案:

答案 0 :(得分:0)

通过调用new Post_Like();,您正在创建类的实例,执行构造函数。

如果你想避免这种情况,并且你已将其定义为单例,则可以将构造函数设为私有。

尝试以下方法:

if(!class_exists('Post_Like')) {

    class Post_Like {

        static private $instance = null;

        static public function getInstance() {
            if(self::$instance == null) {
                self::$instance = new self;
            }
            return self::$instance;
        }

        private function __construct() {
            $this->define_constants();
            // Load public/admin CSS and JS.
            add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
            add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
            // register actions for ajax
            add_action( 'wp_ajax_nopriv_post_like', array($this, 'post_like') );
            add_action( 'wp_ajax_post_like', array($this, 'post_like') );
        }

        public function define_constants() {
            define('POST_LIKE_PATH', trailingslashit(str_replace('\\', '/', dirname(__FILE__))));
            define('POST_LIKE_URL', site_url(str_replace(trailingslashit(str_replace('\\', '/',ABSPATH)), '', POST_LIKE_PATH)));
        }


        public function enqueue_scripts() {
            // enqueue js scripts
        }

        public function post_like() {
            // do some stuff
        }


        public function post_already_like() {
            // do some stuff
        }

        // I want to wrap this function to use it anywhere easily
        public function get_post_like() {
            // do some stuff
        }

    }

}

// new Post_Like(); <-- unnecessary

// you need to check for a function called 'get_post_like' rather than the class name
if(!function_exists('get_post_like')) {
    function get_post_like() { 
        return Post_Like::getInstance()->get_post_like();
    }

}