Jump to content

classes in separate files?


sylvertwst

Recommended Posts

hello all,

i was wondering what you would consider best practise to do: when i have multiple classes, i was thinking of making one php file per category, for example i would have one database connection class and a query class in dbconnect.php, and then user class and login class in user.php. is this a good way of doing things? would yousuggest a new document for each class used? one document for all classes in a single class library?

 

thanx in advance

beginner OO php

Sylvertwyst

Link to comment
https://forums.phpfreaks.com/topic/130987-classes-in-separate-files/
Share on other sites

cool! thanks for the tip on autoload.

 

when i make a new class that implements another class or interface, can i use separate files as well, do i need to use the autoload function at the top of eacth class file then? or should parent/child classes go in the same document still?

I have this in autoload.php

 

function classAutoload($class_name) {
require_once("class.".$class_name.".php");
}

spl_autoload_register('classAutoload');

 

and then I just include('autoload.php') on top of each script. Of course all my classes are in files called 'class.classname.php'

 

 

Classes extending parent classes should have their own files.

on a side note, did you just tell php to use classAutoload() instead of __autoload() to autoload the classes?

 

Almost. This way you can actually have more than autoload function, and PHP will try to use all of them one after another. Useful if you use some third party code, that use autoloading as well.

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.