We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin Class Overview (Multiple Instances?)

Development and Programming

ocallagh's avatar
ocallagh
18 posts
17 years ago
ocallagh's avatar ocallagh

Hey, Looking for some clarification on how plugins work.

From what I can see you have the Class Name, a constructor and methods.

eg:

class User { //Start Class

 var $name="";
 var $email = "";




 function User(){ //constructor 
 
  //Do some stuff, maybe access $SESS to get currently logged in user or query a database. 
  //For the mean time we will just hard code name and email

  $this->name = "John";
  $this->email = "[email protected]";

 }




 function getName(){ //Method

  return $this->name;

 }




 function getEmail(){ //Method

  return $this->email;

 }




} //End Class

Now, I want to access this in my template.

{exp:user:getname}
{exp:user:getemail}

This works fine, however it seems to create multiple instances of the User class. If I was doing some memory intensive stuff in my constructor this would be a massive waste of resources.

Is it possible to declare your class, and then separately access the methods from within that instance of the class… ie:

{exp:user}

{getname}
{getemail}

{/exp:user}

Sorry if this is a really stupid/simple question.. I’ve tried the second approach and I can’t get it to work.

Mod Edit: Moved to Plugins: Technical Assistance

       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

You could do something like that, yes. For example:

class User
{
    var $return_data;

    function User()
    {
        global $TMPL;

        foreach($TMPL->var_single AS $method)
        {
            if(method_exists($this, $method))
            {
                $TMPL->tagdata = $TMPL->swap_var_single($method, $this->$method(), $TMPL->tagdata);
            }
        }
        return $this->return_data = $TMPL->tagdata;
    }

    function getname()
    {
        return "John Doe";
    }

    function getemail()
    {
        return "[email protected]";
    }
}
       
ocallagh's avatar
ocallagh
18 posts
17 years ago
ocallagh's avatar ocallagh

Excellent, Thanks for such a complete solution (along with working code example!)

It seems there is a lot I need to learn in regards plugins.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.