Set apart functions [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What is good way:
Keep all functions in one file or to set apart them? (like database.php, session.php)

My approach is split functionalities into classes; and then to put each class into one file.
If you have a lot of functions, I would do the same - split them into thematically separate files and include them only when needed.
If you work with classes, you can make use of PHP's autoloader functionality that will automatically load any PHP files needed to instantiate a certain class.

I prefer using several distinct files -- generally, one per "group of features".
Advantages :
smaller files : easier to deal with in your IDE
several files : one developper can work on one feature, in one file, the other developper on another feature, in another file ; limits the risks of conflicts (yes, even with SVN and equivalents)
you only need to include what's needed
(And if you extend your question to classes : one class per file, structured in directories ; and using autoloading to include the files that are necessary)

Related

PHP - All classes and function in a single page [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
On php, i want to put all classes and functions on a single page. This page will be called throughout my project. is it recommendable or a bad practice?
It is bad practice. It is called a big ball of mud anti pattern. Try to keep everything modularized. It will be more maintainable and readable. You will thank yourself later.
Sure, it's possible. These files are often called "helpers". You could compile all the functions and classes that you need throughout your website in one file (e.g. functions.php). However, you should not forget to properly split your code apart into different files, to prevent creating a big mess. You can then use the functions/classes like so:
require 'functions.php';
$class = new MyClass(); // Use a class from the functions.php
myFunction(); // Use a function from the functions.php
But! Note that this file can now also be accessed via a URL. For example: http://example.com/functions.php. This can, in some cases, cause unwanted behavior. You can restrict the "direct access" to this file using .htaccess rules or simple place this code on top of the helper file:
if(count(get_included_files()) == 1) die();

PHP Performance optimization [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to optimise PHP code to manage code easily and to speedup page execution time. My strategy is to create a file core_functions.php and defined all functions in this file. Then include this file into every other file of the project.
I need to know is it best practice to have all function defined in one file or it is best practice to split the function into different files and include where needed.
What you're doing is actually going to hurt performance. In each page that loads the 6000 line file the PHP interpreter has to interpret that whole file. You'd be better of splitting your functions into modules and have each page only reference the modules it needs.
This reinterpretation of PHP files on each page load can be reduced though by using a caching mechanism that caches the compiled byte code of interpreted files.
There is a list of frameworks that do this here: http://en.wikipedia.org/wiki/List_of_PHP_accelerators

How the twig caching exactly work? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have recently start work with twig in my project,
I have check that twig have create cache folder, and check that twig basically create some php classes for cache,
I want to understand exactly how this mechanism is work, how it beneficial to reduce the page loading time, any one can help me ?
Twig evaluation process looks like:
Your templates are parsed, abstract syntax tree (AST) is built from then and then AST is transformed into php code
Php code from the step #1 is evaluated
With Twig templates caching you eliminate step #1, since the transformation results are written to a cache files.
So it is highly recommended to use it on production. Just make sure you clear the template directory cache on deploy or specify another directory as a target. It could be a good idea to include the application version number in the path.

Using Java-Style Namespace Convention in PHP or is it better to use X? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I know that Namespaces in PHP are relatively new and so there are many possibilities to use them. Because it was not usable on the Server i did define my classnames like folder_folder_classname if they where in /folder/folder/classname.php
Now i want to change that to get better class names. I thought to redesign my class structure to a java based one, like com\domain\project\folder\folder\class (e.g. in java: com.domain.project.folder.folder package structure) and was thinking that this is a nice solution to avoid any problems with other classes i ever use and don't think about include() for my classes any more :)
So now my Questions:
Is this a good idea or should I use a better system for my php classes?
Is there a common handling for that in php?
(I saw that Zend Framework uses e.g. instead of Zend_Controller_Request_Abstract the namespace Zend\Controller\Request with class name RequestAbstract -> no domain and double wording...)
thanks for your answers :)
I would definitely avoid using the domain wording and just prepend the project name itself like Zend does (that way you avoid collision, too). Since you are thinking about restructuring and if you are using PHP > 5.3.0 (which you should) you might also want to consider the PHP namespaces.

PHP: Class segmentation? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I'm building an Authentification library that's going to have around 45+ methods for dealing with user related stuff. However I've been wondering if it's actually recommendable to keep everything on a single file.
Is there a benefit on splitting my class into several subclasses and load them when needed?
I can always for example split the class into "mandatory" elements and the elements that only registered users need...
For example:
Mandatory Methods:
$user->is_logged()
$user->login()
$user->register()
Methods for Register...
Methods for logged-in user.
It just depends on how you want to be including the class file(s) in your pages. If you want one simply include() statement for every page, then keep it all in one file. Unless your library is HUGE, the overhead from the other classes shouldn't be too much.
If you do it the other way, you'll simply be including different files based on the status of the session of the client.
Personally, I'd split them up as it's easier to edit them that way, but it's totally up to you.
I'd go with the class/sub-class option.
You could then use a factory to return the correct type of user object based on the current URL or by simply specifying the desired type of user object if your particular setup doesn't lend itself to this.

Categories