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
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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.