When is require ever preferred over require_once in PHP? [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
The way I see it, if a file has been included once then any object, function or member in it is defined.
require_once checks if the file is included and if so, doesn't include it again. But when would the event ever arise that someone would go 'this file has already been included so I can use the class inside it, but I'd better include it again because this script needs it'?
Am I missing something?

I use include_once / require_once for classes files, and include / require for html code (let's say a form for example). You shouldn't redeclare the class, but you could insert html code multiple times in your code.

In ye olde days before proper classes, I would sometimes use require inside a for or while loop when doing large imports. Along the same lines, template engines use it for repeated includes of the same file that should fail if the file is missing.

Related

Cannot require two classes in php [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 2 years ago.
Improve this question
I am currently working on a little website and i am trying to require two classes. When I only require one class, everything works fine but when I require two classes my page is not working any more.
This code works:
<?php
require '../assets/php/mapManager.php';
$mapManager = new mapManager();
?>
This code works:
<?php
require '../assets/php/accountManager.php';
$accountManager = new accountManager();
?>
This code works not:
<?php
require '../assets/php/mapManager.php';
require '../assets/php/accountManager.php';
$mapManager = new mapManager();
$accountManager = new accountManager();
?>
My hypothesis is that each of those class files includes or requires the same file containing a parent Manager class or some other common dependency, and you're getting a fatal error when you try to redeclare the class in that file. You can fix this by using require_once instead of require, or ideally by implementing autoloading.
Normally I wouldn't post a "guess" answer, but based on the context and naming and the way the error is occurring I think this is extremely likely, and if it doesn't happen to be the problem in your case, it definitely will be for someone else with the same question.

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();

Best way to manage multiple php includes? [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 6 years ago.
Improve this question
With my current workflow, when I need to add a new CSS sheet or a new script to a 10 pages website, I need to do this 10 times. Same story if I need to rename one item in my navigation menu, or correct a typo in the footer.
That made me realize that I need to start learning PHP to handle that via includes, echo, etc.
Therefore my question is the following: is there a best practice to do this? (I guess this is relatively easy to implement a < ?php include 'header.php'; ?> and a < ?php include 'footer'; ?> but can become quickly messy when it comes to dealing with unique page title and descriptions, etc.
What are your suggestions?
Many thanks
If your building not object style PHP website using require_once seems just the proper way to achieve your goal. It's better than include because require spread all errors and _once only include it once (Even if there is an inclusion of the same file deeper in the including tree) :)
Elsewhere Assetic is library that handle exactly this :)

Have multiple includes in a php file [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 trying to call 2 functions that i have in 2 separate .php files. Let's say that function1() is in my_functions1.php and function2() is in my_functions2.php.
lets say i have in the same folder a third file called main.php and i want to include the functions from both my_functions1.php and my_functions2.php.
Something like this
<?php
include 'my_functions1.php';
include 'my_functions2.php';
function1();
function2();
?>
For some reason i can't have 2 include files. I am kind of new in PHP and it seems that there is nothing on google about this except of putting all the functions in one file.
Try using include_once. You might be accidentally including the same file multiple times.

How to separate this PHP and HTML source [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
I have a change_username.php which makes it possible for users to change their username. If they changed their username a page like "Your new username ist xxx" or "xxx is already used" will be displayed.
I don't want to use an echo and put all the HTML stuff in there.
My first idea would be to create another 2 PHP's which then get the username from the change_username.php and display a proper result with HTML.
Are there any alternatives / better ways to do that? Creating files for every result sucks as much as putting the HTML data in an echo.
If I understood your question correctly you want to include a page with in your PHP: try using this as reference http://www.w3schools.com/php/php_includes.asp
You can include the page using
include 'filename';
or
<?php include 'menu.php'; ?>
I hope this helps you
I would do it a little bit differently, I think the problem is with managing the code where large page is getting messy. What I do in these cases I just use functions with parameters. So I create a lib.php file, where I store function with HTML blocks of code being echoed. Then when I need I call these functions in the necessary files, just requiring lib.php at the top of my page.

Categories