-1

Quick question, I have:

include_once("connection.php"); 

within my header and then on my internal pages I have:

<?php include 'header.php';?>

Do I still need to add:

include_once("connection.php"); 

on my internal pages? The reason I ask is: Right now I only have it within the header and sometimes my forms will save to the database and sometimes they will not. I'm just trying to find out what the best practice is.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
user1235709
  • 43
  • 1
  • 7
  • 3
    best practice is useing an `autoloader` / `bootstrapping your application` and stuff like that. But as long you use `include_once` you can paste it any file you want. `include_once` includes files just once. – JustOnUnderMillions Feb 23 '17 at 16:23
  • I Fully agree with @justonundermillions, the only other input i can add is you could also use `require_once` – Option Feb 23 '17 at 16:25
  • You better not include it in all other files and just in your main file ! –  Feb 23 '17 at 16:25
  • As a personal "style thing" I tend to use `require_once` for PHP things that are required (e.g. database connection configuration file) and `include` (or `include_once`) for display things (e.g. `.phtml` files); `require_*` will cause the application to halt if the required files are missing, `include_*` won't. – CD001 Feb 23 '17 at 16:26
  • use a conditional statement then – Funk Forty Niner Feb 23 '17 at 16:27
  • 3
    this question's starting to look unclear/too broad/opinion-based. – Funk Forty Niner Feb 23 '17 at 16:28
  • Be careful when using require ! if you will have any kind of error in your file, your entire script will stop working ! It means when you edit a file which is required somewhere in your app, you should make sure it work correctly ! however, if you use include, even if you have some kind of errors, your entire app will not stop working ! –  Feb 23 '17 at 16:29
  • Yeah the short answer is - if `header.php` includes `connection.php` and `header.php` itself is included on every page - then you should never have to include `connection.php` again. – CD001 Feb 23 '17 at 16:30
  • 1
    @Soheyl - that's kind of **exactly** the point... – CD001 Feb 23 '17 at 16:30
  • @CD001 and that's why I said to be carful ! because when you work on a real project and you have files included or required everywhere, some times you can't remember or pay attentions to everything before you see the problem when editing something ! So using require should really limited to less possible in the area where you are sure you will not do updates regularly or when you do updates, you know what exactly you are doing ! –  Feb 23 '17 at 16:36
  • *"Right now I only have it within the header and sometimes my forms will save to the database and sometimes they will not."* - again; "conditional statement". Include the file if processing is to done, and don't include if there isn't. A simple `if/else` will do or a `switch case`. This I feel is what the question's about. Btw, you have answers below. If my comment doesn't "answer" the question, you'll need to take it up with the answers, or post your full code. – Funk Forty Niner Feb 23 '17 at 16:40
  • ...and your silence isn't helping. I've left this question, good luck. – Funk Forty Niner Feb 23 '17 at 16:44
  • @Soheyl - put all the absolutely, mandatory `require_once` statements in a sort of single bootup file; use an autoloader for additional classes that may or may not be used on any given page. In essence, if something is required then the application *should* stop if it's missing - I'd argue that the database connection (which the question appears to be about) would be absolutely necessary and should be required. – CD001 Feb 23 '17 at 16:46
  • @CD001 i'm agree with you, I just said all this regarding experience I had by using a CMS ! –  Feb 23 '17 at 16:49
  • @Soheyl - ahhh - there's definitely something to be said for writing bespoke applications; you get to do things that make sense to you :) Unfortunately the company I work for has begun moving everything onto Magento which makes life *"interesting"* – CD001 Feb 23 '17 at 16:56

3 Answers3

0

No, includes are made "recursively".

FYI : "include" is faster than "include_once" because it doesn't check for included files

zenko
  • 147
  • 1
  • 8
  • What? Does that mean? – JustOnUnderMillions Feb 23 '17 at 16:25
  • 1
    what if they "need" to Include "once"? – Funk Forty Niner Feb 23 '17 at 16:26
  • It means that a.php included in file b.php will be recursively included in c.php if c.php only include b.php. EDIT : recursively might not be the proper word... – zenko Feb 23 '17 at 16:27
  • `will` Hopfully i will miss that rabbit hole ;-) – JustOnUnderMillions Feb 23 '17 at 16:28
  • @Fred-ii- : include_once just check the file has not been already included. How can you "need" include_once instead of "include" ? include_once would be this : `function my_include_once($file){static $files;if(!isset($files[$file])){include $file; $files[$file] = true;}}` – zenko Feb 23 '17 at 16:29
  • class-files or configuration-files or function-files should not include other files when you are coding procedual! so a will b will c will a should never happen. Some Basic rules needed for not getting into great caos :-) – JustOnUnderMillions Feb 23 '17 at 16:30
  • And you are using `include_once` for preventing include of a file more than once. php is a template script language .... a file called `connection.php` should only be included once. To make the great `$connection` variable accessable – JustOnUnderMillions Feb 23 '17 at 16:31
  • @JustOnUnderMillions : The thing I don't understand is that include is made by the developper(s) only (not the user) so a file included more than they need to is a developper error. I never use include_once. Several includes error happened to me but I just remove the instruction(s) for the specific script(s). This is my opinion and the way I work, I don't want to debate, just explaining how I do. – zenko Feb 24 '17 at 10:04
  • @zenko Keep in mind that php is an language that grows day by day, and many stuff like good object orientated programming is made be developers that have they own experience and way to do things. But in early days (& still today), where most of the php code was procedual coded, it was an nice option to ensure that a given file is only included once. If you code fully oop with autoloader, your code may uses only 2 time include. The include of the autoloader and the use of include in the autoloader. Your use `'header.php';` is kinda oldschool too (no use of a framework, still procedual). – JustOnUnderMillions Feb 24 '17 at 11:06
  • @zenko Here you can read how stuff like database connections are made/handled with an Dependency Injection Container (OOP) http://pimple.sensiolabs.org/ – JustOnUnderMillions Feb 24 '17 at 11:11
  • Also OOP vs Procedual http://stackoverflow.com/questions/1530868/simple-explanation-php-oop-vs-procedural – JustOnUnderMillions Feb 24 '17 at 11:15
  • @JustOnUnderMillions : include(_once) and Procedural vs OOP are not related. Here, I was just answerng the question about includes inside included files. Why are you talking about OOP vs Procedural ? I don't see the point. Btw, excuse me but you are far away from the reality. I use OOP for many years now and can't stand Procedural. ^^ For web, I use Phalcon (I think it is the least worst (<-- not sure about this meaning) of available web frameworks nowadays). And at work, my personal framework. Both use DI, ORM, templates, ... And my framework uses include, not once include_once. :) – zenko Feb 24 '17 at 12:30
  • @zenko oh and i thought you are the OP ;-) `OOP vs Procedural ?` Because mostly (if not only) in procedural you are useing `include` (oftern in global space) and as i noted in good OOP you will not. Hopefully you see it know :-) `far away from the reality` Nope, the difference is more that i talk about `include/include_once` togehter. Its like the `SQLInjection VS PreparedQuerys` if you use `prepared` sqlinjection-topic becomes obsolet. Same when using good/modern oop, then `include` _can_ become obsolet. – JustOnUnderMillions Feb 24 '17 at 16:33
  • @zenko And if you read my first comment on the OP Question and see the 3 upvotes.... do you still thing its far away? – JustOnUnderMillions Feb 24 '17 at 16:35
  • @JustOnUnderMillions In my comment, "far away" is about my kind of programming, not your comment. ;-) I don't use procedural or 'header.php'. However, I maintain I prefer the use of include instead of include_once because it's faster and prevent "include_oncing" everything everywhere... It's clearer to me. Moreover, I just told that include is faster thant include_once, and I don't use include_once. Nothing more, I'm not trying to explain that include is better than *_once, It depends on the developper to make his choice. It's like "include or require" I never use require(_once) either... – zenko Feb 27 '17 at 09:24
  • @JustOnUnderMillions : When answering Fred-ii, I asked how is it possible to need "include_once" because I think that if your app's architecture is correctely done, the need of include_once is not justified, including a file once is good enough. – zenko Feb 27 '17 at 09:30
0

I usually do it manually, with a call to require() instead:

index.php

require("Config.php")
$c = Config();

Config.php

<?php
    if(!DEFINED("CLASS_CONFIG_PHP__")) {
        DEFINE("CLASS_CONFIG_PHP__", 1);
        // All library code here
    }
?>

This way, I'm sure everything is only defined once, and require will make sure the included file has no errors (will fail at that line if there are errors in it).

SsJVasto
  • 486
  • 2
  • 13
0

include_once() will include a file only once regardless of how many times you call it with the same parameter. include() will throw an error if its called twice with the same parameter.

if you have include('connection.php') in your header and all internal pages use this header then you wont need to include this in internal pages too.

However, it is bad practice to mix view layer with business logic layer. Read a bit on MVC patterns and how to use it. Connection should be done in a back end where all the database functions are called. Once header.php is called then you are in rendering mode and you should only be rendering content at that stage.

Tim Hysniu
  • 1,446
  • 13
  • 24