0

I'm trying to build up my knowledge of OOP in PHP. Every suggestion I've read says that "all php classes should be stored inside a separate file than the index.php file". One went as far as to say

"You would never create your PHP classes directly inside your main php pages - that would defeat one of the purposes of object oriented PHP in the first place!"

I'm trying to work out why that is.

I understand that flicking through many lines of code in a single file isn't to everyone's taste. However, I've always preferred to load all my functions in to different sections of the same file as much as possible. I haven't found any problems associated with this before so I'm not sure why convention is to hold all functions elsewhere.

My question is: Is this just a convention, or is there another specific benefit to organising files in this way when using OOP?

This question arises because I built a heavily function based (but not class based - as I'm learning) project with over 150k lines of code that draws on php libraries and the db connection file but nothing else. That project includes various modules that I use to template / construct new projects, and I'm fond of it. I want to bring it up to date with the latest standards, but unless there is a real benefit to restructuring the files, I'm not sure it is worth it.

3
  • 4
    One important reason is autoloading. github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md Commented Jan 8, 2014 at 13:46
  • Moving classes to their own file has nothing to do with the premise of OOP. It is good practice, though. It adds a clean layer of separation; as well as various other benefits. Commented Jan 8, 2014 at 13:47
  • A real benefit is when you use an autoloader to only load the classes you need when you actually need them, and you see the drop in memory Commented Jan 8, 2014 at 13:47

1 Answer 1

2

One major benefit of splitting files into logical units (with only one class per file in OOP) is to allow component code to be reusable in another context without having to "pull" unnecessary code.

Let's say you've written an amazing application which loads things from a database, does some clever transformation on the data and then displays this in a table. You can indeed write everything in one file and that will work; you can even write everything in the rendering page.

But if you need another page to transform an array into an html table, and you realise that you have already written a function (or an object) doing the exact same thing in the first example (in the 'display table' bit), you would obviously want to "reuse" this function/object.

The obvious way would be to 'require' the file containing the code you want to reuse. Unfortunately, because you wrote everything in one page when you require the code to transform an array to a table the code will start by loading things from the database - which you don't need at all. You are only interested in the table generation bit.

Secondly, You can copy/paste the function/object to your new file. This is the worst solution, if you discover later a bug in the table transformation function, you'll have to fix in BOTH file. Chances are you would have forgot to update the other one.

Thirdly, you could extracting the required code into a new file and make both "pages" that need to use it 'require' it.

The fourth solution is to write each class in its own file; This doesn't hurt and it's much much simpler than extracting code when required.

That's the first major benefit. Added to that, it allows you (as others have said) to use autoloading - and make code navigation easier. Finally, if there is a problem in class A, I know that I can find it in file 'a' without having to browse all the file.

Sign up to request clarification or add additional context in comments.

1 Comment

The issue of duplication is easily solved by including in the main body of the php file a long list of functions - one after the other, before attempting to display the table. The code that I need to duplicate I simply call with the line: newfunction(); at the second instance... that means I don't duplicate any code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.