特征中的__callStatic php无效

时间:2014-05-15 18:02:34

标签: php singleton traits

是否有可能在这个特性中使用callStatic魔术方法如何拥有它

<?php

namespace INSP\Traits;


/**
 * Class Singleton
 *
 * @package INSP\Traits
 */
trait Singleton
{
/**
 * @var
 */
private static $instance;

/**
 *
 */
public static function init()
{
    return self::getInstance();
}

/**
 * @return Singleton
 */
public static function getInstance()
{
    if (! (self::$instance instanceof self)) {
        self::$instance = new self;
    }

    return self::$instance;
}

/**
 * Prevent the class from being woken up
 */
final private function __wakeup()
{

}

/**
 * Prevent the instance from being cloned
 */
final private function __clone()
{

}

/**
 * Allow calling of nonstatic methods in a static look
 *
 * @param $method
 * @param $args
 *
 * @return mixed
 */
public static function __callStatic($method, $args)
{
    echo 'running' . $method;
    return call_user_func(array(self::getInstance(), $method), $args);
}

}

目标是能够将非静态方法作为静态方法调用,并捕获它通过getInstance方法运行并链接请求的方法。例如。 输出方法见下文

    <?php
 */

namespace INSP\Multimedia;


use INSP\Core;
use INSP\Traits\Singleton;

/**
 * Class Brandscape
 *
 * @package INSP\Multimedia
 */
class Brandscape
{
    use Singleton;

    /**
     * @var
     */
    private $id;

    /**
     * @var object
     */
    private $fields;
    /**
     * @var object
     */
    private $options;

    /**
     * @var
     */
    private $brandColor;

    /**
     * @var
     */
    private $brandScape;

    /**
     * @param $id
     */
    private function __construct($id = false)
    {
        if (! $id) {
            $this->id = Core::getId();
        } else {
            $this->id = $id;
        }

        if ($this->isValidPage() && $this->isValidPostType()) {
            $this->fields = (object) get_fields($this->id);
            $this->options = (object) get_fields('options');

            if (empty($this->fields->background_color)) {
                $this->brandColor = $this->options->global_background;
            } else {
                $this->brandColor = $this->fields->background_color;
            }

            if (empty($this->fields->global_background)) {
                $this->brandScape = $this->options->global_background_image;
            } else {
                $this->brandScape = $this->fields->global_background;
            }
        }
    }

    /**
     * @return bool
     */
    private function isValidPostType()
    {
        return in_array(get_post_type($this->id), get_field('brandscape_post_types', 'options'));
    }

    /**
     * @return bool
     */
    private function isValidPage()
    {
        return is_archive() || is_search() || is_front_page() || is_404();
    }

    /**
     *
     */
    public function output()
    {
        echo <<<HTML
                <style>
                    body {
                        background: none !important;
                        background-color: {$this->brandColor} !important;
                    }
                </style>

                <div class="showBrandScape">
                    <img src="{$this->brandScape}" />
                </div>
HTML;

    }
}

0 个答案:

没有答案