PHP - All classes and function in a single page [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 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();

Related

Is it bad practice to make all of my web pages .PHP pages? [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 2 years ago.
Improve this question
Is it bad practice to make all of my web pages .PHP pages? My main reason for doing this would be to prevent myself from writing repeating code for things such as my nav bar and footer and having these in separate files so that I can 'include' them in a way similar to below.
<?php include('nav.php'); ?>
I was made aware that you can also use JavaScript to do this however my thoughts are that relying on JavaScript for something as crucial as a nav bar would be a bad idea.
This is more of an opinion question than anything. I would not consider this to be bad practice as long as the following conditions are met:
If someone browses to https://example.com/nav.php or any similar "include only" nothing bad can happen as a side-effect. By bad I mean that if the .php expects to be included from wider context and ends up wiping the database for example. Easy way to prevent this is to declare a constant in your main "page" files and check that in the includes.
You find this approach easy to manage. In a larger page you might end up with a lot of files that are included from different places and expect certain global variables to be present. In these cases structuring your directories correctly and documenting the code is essential.
Happy coding!

Is-it worth to split a page into many pages? [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 8 years ago.
Improve this question
I've got a question about php and pages :
Is-it worth to split the code into many other files ?
So at the end, the index.php would be composed of :
include 'footer.php';
include 'header.php';
include 'home.php';
include 'load.php';
[...]
Or should I put all the code in an unique page ?
Other question, if I include a php page, could all the vars created in the included page be used in the main page, or should-I use $_SESSION ?
Thank you all for your help !
As for your main question it’s worth. In fact, it will be a pain for you to manage your web page if you don’t follow this method in the long run. I am not a php guy but I can tell you this because this is a universal truth. The idea here is to write once and use anywhere necessary. You don’t want to write the same thing again and again, do you? That’s why we produce function when programming. We define a function and use it as many times we want. Now consider you have suddenly found that you need some modification to do , as you have created a function you can make the change in just one place and it will be reflected everywhere without touching a single line of code outside the function’s code. In actual project change is a constant. That’s why we find many design pattern in the software industry like MVC.
In a web project, mostly (not always) the header, navigation, footer are same across all the pages. Therefore, you should consider making different file for these stuff and you should always put your content in your index.php file which are unique for index page.

"Publish" PHP functions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am still pretty new to PHP but I was reading through WordPress pages to get ideas for my own site and saw <?php get_header(); ?> and I remembered that I have always wondered how the developers of WordPress made these "public functions"
There is probably a real name for them and they probably aren't even functions but I have been wondering if I could create something like this for my own website. I know you can use <?php include 'header.php'; ?> but what if for some reason I want to be really lazy or have long file names/paths? Could I make it simple and quick?
For your own website, you can create functions that wrap long file names for includes. This approach can work whether you are encapsulating the function in a class, or if you are leaving it in the global namespace (as others have suggested...that's another topic for debate).
If writing your function in the global namespace, you may want to consider wrapping your function declaration in an IF statement that checks to see if the function has already been declared. This might allow other developers to replace your method with their own (for example if you are including a view or something visual they want to replace).

Set apart functions [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 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)

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