简体   繁体   中英

How would you structure, or the best way to structure the code in PHP?

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)

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM