Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using a class to retrieve values from custom tables that I need to use in different files of my WordPress Theme and im not sure which is the best approach for this.

This is the class i got right now (dont blame as im a bit noob :D)

class Plans {

    public $free_plans;
    public $purchased_plans;

    private $free_count;
    private $purchased_count;


    public function __construct(){
        global $current_user;
        $this->user_id= $current_user->ID;
        $this->free_plans();
        $this->purchased_plans();
    }
        public function free_plans($debug=false){
        global $wpdb;
        $free_plans =  database query here;
        $this->free_count= count($free_plans);
        if($debug) $this->debug_wpdb(); 
        $this->free_plans= $free_plans;
    }

    public function purchased_plans($debug=false){
        global $wpdb;
        $plans_purchased= database query here;
        $this->purchased_count=count($plans_purchased);
        if($debug) $this->debug_wpdb(); 
        $this->purchased_plans= $plans_purchased;
    }

    private function debug_wpdb(){
        global $wpdb;
            $wpdb->show_errors();  
            $wpdb->print_error();

    }

    public function get_pplans_count(){

            return $this->purchased_count;
    }

    public function get_fplans_count(){

            return $this->free_count;
    }

    public function get_purchased_plans(){

            return $this->purchased_plans;
    }

    public function get_free_plans(){

            return $this->free_plans;
    }

}

I need to use this functions all around my theme (header.php , page-custom.php, etc) and i just want to use one instance of the class as i dont want to query database every time i need one value because querying just two like i do when i create the class is enough.

Now where should i call this class? I thought that by creating the class in functions.php....

$p= new Plans();

.... I was going to be able to access $p everywhere but it seems that im wrong. What would be the best approach ?

On my case for example i need in header.php

$p->get_fplans_count();

On page-plans.php

$p->get_purchased_plans();

And so on....

EDIT: I dont want php help, i just want to know how to use a php class properly in WordPress. It seems that its a bit confusing for me to explain it (English is not my native language) Really Thanks for all your help

share|improve this question
    
This is a PHP question, not a WordPress question. –  Chip Bennett Oct 18 '11 at 12:53
    
No, its not. Its a question about how to use php inside WordPress. Or WordPress its not using JavaScript, php, etc ? I just dont know how to implement the class inside WordPress. Please read carefully before voting me down –  chifliiiii Oct 18 '11 at 12:57
    
Your question is about how to call a PHP class, and is entirely platform-agnostic. WordPress does not directly or indirectly impact the answer to this question. –  Chip Bennett Oct 18 '11 at 12:59
1  
Although I do agree with Chip a bit, I will say that w3prodigy.com/web-production/behind-wordpress/… this will give you some insight, this codex.wordpress.org/Writing_a_Plugin, devlounge.net/code/structure-of-a-wordpress-plugin and striderweb.com/nerdaphernalia/2008/07/… and pretty much everything else on Google. Having a globally accessible instance is an acceptable practice, but don't call it $p, too short, may fall victim of collisions. –  soulseekah Oct 18 '11 at 13:09
1  
Also, I'd suggest you download some of the good plugins (esp. those written by maintainers) and look at how they do it. –  soulseekah Oct 18 '11 at 13:14
show 2 more comments

2 Answers

up vote 4 down vote accepted

The easiest way to have access to your variable in other files is to do exactly what Chip suggested, globalize the variable.

This is the same thing WordPress does to make global variables like $wpdb available in other files. I can already see you're making calls to the global $wpdb variable in your class. You need to do the same thing.

When creating an instance in functions.php:

global $p;
$p = new Plans();

When referencing the global instance in header.php, footer.php, etc:

global $p;
$p->get_purchased_plans();

With this in mind, I want to clear up two things:

This is not a WordPress question

Yes, you're using WordPress. And yes, you're trying to figure out how to use a class within WordPress. But this question is in no way specific to WordPress. If you were building a website using just PHP (i.e. a single index.php file that includes header.php and footer.php and makes dynamic references) you'd still run into the same problem.

You're question is about variable scoping, not about WordPress. Chip was 100% correct in calling this out earlier. I'm explaining it here rather than in a question so that you'll explain why.

When you call $p = new Plans() in your functions.php file, you're creating an instance of the Plans object and storing it in a local variable called $p. If that variable is defined within a function (as it should be), then that variable is local to that function. If it's defined within a class, it's local to that class.

Occasionally, you can accidentally define a variable in the global scope ... this is a mistake made often by developers new to PHP or who are just getting started with object-oriented programming. It's not a bad thing per se, but a bad habit to fall in to.

If you need access to your variable outside of the immediately local scope, always always always use the global keyword to make it available. That said, make sure you also name your global variable in such a way that name collision is minimized.

$p isn't very unique. It's very possible that a plugin (or WordPress itself for that matter) could redefine $p as something other than an instance of Plans() later down the road. That would break your theme ... it should be avoided at all costs!

Something along the lines of $chifiliii_plans_p would be a better choice because I highly doubt any other system would also choose that name.

You're doing it wrong

1 - Custom tables are BAD

It's bad form to use custom tables in WordPress. There are very few cases where custom tables are warranted in the system, and in 6 years working with the platform I have only ever come across 1 or 2 reasons to have them.

If your theme is meant to be distributed, remember that some people won't be able to use it. Several security consultants recommend that the user through which WordPress access the database be limited in its functionality - in many shared systems, it can't create tables in the first place.

In multisite systems, you're not just adding new tables for one site, you're adding new tables for every site that activates your theme. This drastically increases the size of the database, which might be a problem for some users (though not all).

If you're running custom queries directly against the database, you're taking a lot of security concerns on your own shoulders. WordPress itself is pretty secure ... unless you're doing something wrong, it's very difficult to open yourself up to malicious users who attempt SQL injection and other attacks against your system. If you're running the queries yourself (outside of the WP_Query API), then you need to sanitize those queries to prevent malicious statements yourself.

As it is, WordPress has a fantastic Options API already.

WordPress also supports Custom Post Types for storing custom data.

Don't reinvent the wheel ... use the tools that are already in place to help you out.

2 - Don't include functionality in themes

If you've got this much advanced functionality baked into your theme, you're setting yourself up for failure. What happens if a user deactivates your theme? Simple ... suddenly all of their custom data disappears.

It's been said on this site many times: themes are meant to provide styling for WordPress. Plugins are meant to provide functionality. If you're going to store custom data, do it through a plugin. Use the theme to style the presentation of that custom data.

share|improve this answer
    
Thanks for explaining all these. The problem was a name collision as i commented on my question. I never use custom tables but for this project couldnt find a better way to do it. The theme is used just for one site that is built in the wordpress + buddypress. You are right with plugin , i think i will change my approach next time. Thanks for taking the time to answer this. –  chifliiiii Oct 18 '11 at 14:48
add comment

If you need a variable to be available throughout all template files, globalize it, and call it in header.php:

global $p;    
$p = new Plans();

Just be sure to globalize it wherever you call it:

global $p;    
$p->get_fplans_count();
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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