views:

93

answers:

4

Hi, i want to know the best way (faster and better) to structure the code in PHP.

Some people save a Master Class with functions, sub-classes and var's into an unique global variable, like $modx CMS case.

What are your structure techniques?
And how do you connect to your DB?
Do you all prefer PHP Frameworks like PHPCake?
How do you structure Plubic vars (configuration) and Private vars (temporary or program running vars)?

The reason for my questions is that i wan't start programming staticly, with a strong base. Currently i'm programming with ModX framework/CMS.

NOTE: Please justify each time you vote -1 on a answer!

Thanks in Advance (Sorry for my poor English)

A: 

Use autoload to load classes and access instances through singletons:

class DB extends mysqli{
  private static $instance;
  private function __construct(){
    parent::__construct(DB_HOST,DB_LOGIN,DB_PASS, DB_DB);
    $this->set_charset("utf8"); 
    $this->autocommit(false);
  }

  public static function i()    {
    if (!self::$instance instanceof self){ 
      self::$instance = new self;           
    }
    return self::$instance;
  } 
}

use:

$res=DB::i()->query("SELECT * FROM whatever");

it will create one instance of mysqli and whenever you will access DB::i() it will return that instance.

codez
So, you use class's to create a management on your code flow?
CuSS
A: 

I think you will be interested by dependency injection Also use autoload: it's a killer feature. For the DB, use PDO, which is the native PHP database layer.

Savageman
i didn't knew PDO... Good, and how do you structure your vars?
CuSS
NOTE to the person that voted -1 here: Please justify each time you vote -1 on a answer!
CuSS
I generally wrap my vars inside static classes, to provide a sort of namsepace. Since PHP 5.3 namespaces are native, but many hosting companies may still not support it.As instance, I have this Page class (namespace to store all the vars related to the page) which contains the title, the keywords, the description, the meta robots behavior, and arrays for which css, js and rss are to be included / displayed. Turned out very useful!
Savageman
Hm... interesting... and where do you save the namespace content? Mysql or php file?
CuSS
It gets loaded within the exxecution. If you're displaying a news item, you set the page's title dynamically with something like `Page::$title = $news->title;` If you have a specific CSS for the news module, you add it with something like `Page::$css[] = 'news.css';` (prefer using methods such as `Page::addCss()` or `Page::setTitle()` rather than setting the var directly.This way, your template/layout use the Page class to retrieve and displays the appropriate title / css. Having some centralisation for CSS / JS even allows you to combine/minify them before sending them to the client. ;)
Savageman
A: 

Using a framework is a better way than writing everything from scratch since a lot of what you gonna need have already been done.

Prefer to use frameworks that are based on the MVC pattern. This way your code will be divided into the Model, View and the Controller.

  • The model is where you put the code for manage the data (db, ...)
  • The view is where you put the code for the user interface
  • The controller is where the logic of your application goes.
Ed
Good structure, is that structure versatil?
CuSS
Yes, and it's the default structure for most frameworks
Ed
+2  A: 

Faster and Better do not go together.

Faster

Pure PHP is faster and more compatible. Your distributions will be smaller. Is it the easiest to maintain? Well, it can be if done correctly. Large software projects like Drupal or Wordpress show that Framework-based projects are not any easier to maintain than pure PHP. Pure PHP still has some built-in classes, but these ultimately wrap back to functions. Autoloading is nice but larger codebases still take a performance hit.

Better

Framework-based projects tend to be better to maintain and work with in the long run. They are slower due to their nature (autoloading has a performance hit if you have a lot of different paths in your include path, or a badly ordered include path). It also gives a preset structure so that others can jump in and understand what is going on quicker. Most Zend Framework projects look the same, so you'll have a better chance of finding what piece of code is running. There's a performance hit and external helpers like opcode caches are required.

My Opinion

Go with the 'Better' option: a Framework with autoloading. A lot of the work has been done already for you so you will be out of the gate faster in terms of production time. Right now I suggest the Zend Framework plus Doctrine. Is it the fastest? No, but it is easier to maintain.

If you need flat-out speed, go core PHP or a roll-your-own low-level framework. ZF, Code Igniter, symfony, they all take a performance hit compared to core PHP.

dragonmantank
+1 Very good answer, if no one posts a better answer, i will accept this ;) One thing you didn't answered... What is your preferred structure? Running with normal functions or class functions? How do you preffer to connect to a DB for example, and how do you threat Plubic vars (configuration) and Private vars (temporary or program running vars)?
CuSS