Do you prefer to function or come in one php file?

How do you manage your php codes? Do you prefer to function in a single php file or include larger blocks of "raw code"?

Edit: Actually my code is pretty nasty as I don't use namespaces and classes - just functions and that includes. I'll look the classes up ^^.

0


source to share


8 answers


If you are using php classes this will sort. If you don't, then it's really hard to give an acceptable answer, except that you have to learn. All php code I've seen without classes anyway seems to get messy.



+3


source


I agree that OOP is the way to go. Large solid blocks of code are a maintenance nightmare. Definitely not the way to go. You should split your code into small blocks that interact with each other and are easily customizable on their own.



+3


source


When I used the PHP program, I liked to group common utility functions in a common file for inclusion on most pages and group classes in dedicated files so that they could be loaded only when needed.

+1


source


Use them as you need them. I use include for chunks of large processing code and functions for utility functions. The somethines I use include a function too ... it really depends on how much you like your code.

Consider many of them include more fopen () from the PHP module and they can slow down the entire script. So don't try to add too many inclusions.

+1


source


I usually use functions / classes for logic and include for display. I am getting something like this in the controller ...

case 'widgetlist':
  $widgets = $DAO->getWidgets();   //get some query
  include('view/showWidgets.php'); //assume a global $widgets variable
  break;

      

I found it easier to give the HTML / CSS designer an include rather than a function call that is displayed. The downside is that I rely on globals to pass variables to include rather than arguments, which is much safer.

0


source


I am making the classes in separate files with the correct namespace prefixes (until they are included at least). I also put functions as static methods in "static classes" for the namespace effect.

I use autoload to include files, so I don't have to write hundreds of includes. Save My_Example_Class as {lib} /My/Example/Class.php

0


source


The thing I'm working on includes one file at the top of each page containing all the global functions and database settings. It works as it is, but now I'm moving the functions to separate files because with everything in a big chunk it is completely impractical to do any validation.

0


source


PHP has a __autoLoad () function that you can use to intercept class calls. If the class doesn't already exist, you can set up a simple search code and find the appropriate class code to include. Then it will continue to execute as usual.

0


source







All Articles