3

I am writing some functions to display Custom Fields across multiple templates. Rather than getting each CF like so:

$var = get_post_meta($post->ID, 'my_cf_key', true);

and then checking if the var exists, if so, doing some HTML and then echoing the variable, I have started another approach.

My concern is that this new approach might become more expensive and load on the server... Here it is...

Each CF has a function to a) get it via the above code, then b) echo it. Within each get function, I am calling global $post.

Then, on each actual template page, I am first checking the get function for that CF, then doing the echo function, like so:

if ( func_get_the_cf() ) {
echo '<div>';
func_the_cf();
echo '</div>';
};

So the above looks like it is having to do this for each one... a) get a global $post from the get function and get a value from the DB, then if that exists, run the echo function, which again has to do global post and assign a value from the DB by running the get function.

So it seems like I am doing things in unncessary loops just so I can have clean looking template pages.

is this too much PHP? Am I making this too expensive on the server?

Thanks.. I'm just 5% into the project now so I rather get some opinions before I finish it all the way I'm doing it just to change it all.

Thank you!

  • Why not just create a global class with all your variables pre-initialized? – Twifty Aug 31 '13 at 3:09
  • I am not that skilled in PHP unfortunately and that's a bit over my head. I'd call myself intermediate, but thats all relative I suppose. Just trying to figure out if I've made this too expensive on server or not. Thank you! – user37390 Aug 31 '13 at 3:20
  • Wait a moment, I'll give you a basic class... – Twifty Aug 31 '13 at 3:30
2

Here is a basic class which you can create once:

if ( ! class_exists('my_class') ) :

class my_class {

    public $my_var;

    // This variable is for a single instance
    static $instance;

    function __construct () {
        // Call other methods like this
        $this->initialize();
    }

    function initialize () {
        // Populate your variables here
        $this->my_var = 'any value you want';
    }

    static function get_instance () {
        // Static variables are persistant
        // Notice the 'self::' prefix to access them
        if ( empty(self::$instance) )
            self::$instance = new my_class();
        return self::$instance;
    }    
}

endif;

Now in your template files you can access the class like this:

<?php $var = my_class::get_instance()->my_var; ?>
  • Awesome! Thanks. I had to remove the () in the my_var declaration for the template so it reads just ->my_var. I've added a global $post to the variable section just once and now it'll work great. Thanks again! – user37390 Aug 31 '13 at 3:49
  • welcome. Typo on my part. Just fixed it. – Twifty Aug 31 '13 at 3:50
  • For this scope, using a static variable instead to access the instace variable is more comfortable: static $my_var; instead of public $my_var, then convert initialize method to static: static function initialize() { self::$myvar = 'a value'; }. So __construct contain self::initialize() instead of $this->initialize(). Now when you need variable $myvar = my_class::$myvar;: more compact and intuitive. – gmazzap Aug 31 '13 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.