0

I've a big project so I've created these classes:

  • forms
  • members
  • messages

Now I have a include file that calls for every class and initiate this classes, like so:

require_once('forms.php');
require_once('members.php');
require_once('messages.php');

$forms = new forms();
$members = new members();
$messages = new messages();

And in every class there is the another class that needed like members class as example:

class members() {
      function __construct()
    {
        global $forms;

        $this->forms =  $forms;
    }
    { function list that use $this->forms... }
}

Is there a better way to make the 'forms' class as global or something? Because in every class file I've a few calls to global classes.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Rafael Mor
  • 626
  • 1
  • 7
  • 21
  • 1
    Use an autoloader, look it up. Basically, you set it up so that you only include the classes you instantiate objects from on every file. It's done dynamically, so you needn't to explicitly load files in you application. – Madara's Ghost Feb 04 '13 at 14:05
  • This is preferable in your case: http://php.net/manual/en/language.oop5.autoload.php – Kerem Feb 04 '13 at 14:06
  • If i understand correctly it looks like you need to rearrange things into a dispatch or bootstrap file which can leverage OOP features such as composition and the spl_autoloader_register constructor. Are you using a framework at all or an MVC (Model View Controller) approach? This might help http://php.net/manual/en/language.oop5.autoload.php, note __autoload looks like it will be deprecated in the future, and also this if autoloading is looking like a solution, http://stackoverflow.com/questions/12961422/where-should-autloader-go-in-mvc – Opentuned Feb 04 '13 at 14:20
  • i'm not using framework, iv'e change my code to spl_autoloader_register and it's work great, save me a lot of requirements calls. you think that spl_autoloader_register will be deprecated or only the __autoload() ? – Rafael Mor Feb 04 '13 at 14:35

1 Answers1

3

Step by step :

loading :

require_once('forms.php');
require_once('members.php');
require_once('messages.php');

should be handled by an autoload script given to spl_autoload_register :

spl_autoload_register(function($class){ require($class.'.php');});

A very good practice can be to respect the PSR-0 norm and using composer as autoloader. To be allowed to use PSR-0 be sure you have at least PHP 5.3

What is PSR-0?

It is a recommendation of the PHPFIG to make standard modules which can be included in every php framework easily. It makes a list of rules to follow to be sure your code can be autoloaded in a standard way. So you will use namespaces in sync with the directory architecture.

Composer is a powerful tool which rely on PSR-0 to autoload your class. What's more it has the ability to make a dependency tree to load every component that respect PSR-0, for example every Symfony2 bundle can be loaded independantly from Symfony itself, you will just have to load Symfony components the bundle relies on (example the HTTPFundation or the YamlComponent). If your project depends on the Symfony Yaml Component, in a configuration file called composer.json you will add in the require section

'Symfony\Component\Yaml': *;

and each time you use php composer.phar update it will get the latest version of it and include it to your project.

about dependencies and cycles :

Never use global ! it is a pain to maintain. It has to be used only on despair cases. For yours, you can break the cycle in construction by splitting on assignation in two :

class members() {
  function __construct($forms)
    {


        $this->forms =  $forms;
        $form->setMembers($this);// here is the trick
    }

}

class forms(){
   public function __construct($members = null){
        $this->members = $members;
   }
   public function setMembers(Members $m){
         $this->members = $members;
   }
 }
artragis
  • 3,677
  • 1
  • 18
  • 30