Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@xipx

PHP の `__get` マジックメソッドによる Flyweight 的なことをやる

More than 5 years have passed since last update.

すでに取得済みのデータは再度取得したくない時に役に立つパターンです。
単純ですが、実用的です。PHPのマジックメソッドって便利ですね!

<?php

class Post {
    public function __get( $property ) {
        return $this->$property = $this->$property();
    }

    private function permalink() {
        // ここで重い処理をいろいろやってデータを取得する
        return "My Permalink<br>";
    }
}

$post = new Post();

// `permalink`っていうプロパティがないので `__get` の中で作ってあげる
echo $post->permalink; // 実際は ->  $post->permalink = $post->permalink();

// これ以降は単なるプロパティ参照
echo $post->permalink; // $post->permalink
echo $post->permalink; // $post->permalink

クラスレベルでも同じことをやりたいのですが、残念ながら__getはクラスメソッドには適用されないのです。
メソッドを直接呼ぶ形での実装になってしまいますが… 一応こんな感じでしょうか。


class BaseModel {
    protected static $data_pool = [ ];

    public static function all() {
        $data = static::get_pool_data( 'all' );
        if ( isset( $data ) ) {
            return $data;
        }

        return static::set_pool_data( 'all', 'my data' );
    }

    /**
     * Try to get data from data-pool associated with the $key
     * If there is no data to get then return null
     *
     * @param $key
     * @param $value
     *
     * @return mixed
     */
    protected static function cache( $key, $value ) {
        $data = static::get_pool_data( $key );

        return $data ? $data : static::set_pool_data( $key, $value );
    }

    /**
     * Try to get the data from data-pool
     * if there is no data to get then return null
     *
     * @param null $key
     *
     * @return array|null
     */
    protected static function get_pool_data( $key = null ) {
        // if $key is not supplied return the whole data set
        if ( $key === null ) {
            return static::$data_pool;
        }

        $data_in_pool = null;

        if ( array_key_exists( $key, static::$data_pool ) ) {
            $data_in_pool = static::$data_pool[ $key ];
        }

        return $data_in_pool;
    }

    /**
     * Store arbitrary data to the data-pool
     *
     * @param $key
     * @param $value
     *
     * @return mixed
     */
    protected static function set_pool_data( $key, $value ) {
        static::$data_pool[ $key ] = $value;

        return $value;
    }
}

class Animal extends BaseModel {
    public static function all() {
        $all_animals = 'Bird, Horse, Cat, Rabbit';

        return static::cache( 'all', $all_animals );
    }
}

?>

<h1><?= Animal::all() ?></h1>

<h2><?= Animal::all() ?></h2>
<h2><?= Animal::all() ?></h2>
2
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?