Jump to content

A question of speed and efficiency


coplan

Recommended Posts

I'm working on a modular system -- a specialized CMS, if you will.  There will be any number of modules that could be loaded at any given time.  Some of them won't be in use every page load, and I have a system in place to load them only as needed.  But many of the modules (an indefinite amount), may need to be loaded at every page load. 

 

With that in mind, I want to make sure I'm keeping my eyes out for speed, memory usage and overall efficiency.  The module API for my system is in development now, so this is the time for me to think ahead. 

 

Is there any memory footprint or speed differences between a normal list of functions and an object class containing those same functions? 

 

For example, two scripts.  Is this more efficient?:

$module_some_variable='stuff';
$module_another_variable = 'more stuff';

function foo() {
   $bar = 'yippie';
   return $bar;
}

 

Or is this more efficient?

class module{

   var $some_variable;
   var $some_other_variable;   

   // Constructor
   function module() {
      $this->some_variable = 'stuff';
      $this->some_other_variable = 'other stuff';
   }

   function foo() {
      $bar = 'yippie';
      return $bar;
   }
}

 

Your thoughts are greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/41616-a-question-of-speed-and-efficiency/
Share on other sites

What about if there were hundreds of classes?  Aside from performance reasons, are there specific reasons why classes would be better for a modular system? 

 

My first thought was to do a classless modular system.  Module would have a standard function naming system where the first part is always the module system name but the last part of the name is always standard.  For example, the news module would have functions like "news_module_content()", "news_module_insert()" and "news_module_sidebar()".  A forum module might be named "forum_module_content()" and so on.  If a given module was called upon by the user of the site, the core would call modulename_module_content(), and all the pertinent content would be returned to the core. 

 

If I handled as a class, I would simply take all these functions and stick them in the class, minus the prefix.  My function calls would then be reduced to '$modulename->content()' to get the content.

 

I don't really anticipate much need for global (or class) variables, so I'm not so sure the class would be a huge benefit.  But it would make it easier to keep things organized and standardized.

 

Still on the fence as to my approach.

Use classes.

 

Trust me, they were designed to make your life easier.

 

Have you looked at the "extends" keyword? You can do stuff like make a base class called 'page_base' this will display the basic page layout, title, menus, contents and footer. Then each page of your site extends this object and overrides the display_contents method.

 

Now if you need to change the menu, you change it in the page_base obj, and all the other pages see this change.

 

Now you could also have news_page_base that extends page_base, but this news page has all your news down the left hand side. All the pages in the news section would inherit from news_page_base. Tad-dah, a hierarchy of objects = more power to you.

 

monk.e.boy

I have a lot to learn about classes.  To date, I sorta just use them as places to store specific variables and functions...more like an organization point of view.  I havn't experimented yet with the extends keyword.  But I'll look into that.

 

Thanks again for your help.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.