What is the purpose of using `include` in PHP? - php

What is the purpose of using include in PHP, because if the file does not exist then there is something wrong with either the script or the web servers configuration. Surely it is better to use require so that the problem gets highlighted.
So, what is the purpose of include in the language?

For something that is not required for the page to run successfully.
For example, think about a web page. You have a sidebar containing ads that is shared between pages. As a good developer, you stay DRY and put this in an include file. You wouldn't want the web page to fail (with fatal error) simply because the script couldn't find the file (say your co-worker uploaded it to the wrong spot). As such, you use include instead of require.
There are additional benefits - such as performance - to using include. But that's a real world example which should give you some perspective.

Using require on a non-existant file, a fatal error will be raised, when using include - only a warning. That is the only difference.
These functions are only tools and how they will be used is up to the programmer.

Include simply takes all the content in a specified file.
Using include and require is the most asked question for new in php.
for what i understand require will throw a fatal error when file does not exist and include will throw a warning that state file is not been found or not exist.
This means using using those two have effect on users also. Throwing fatal error may cause panic on user (non-IT) while include may simple say its broken.

Related

How to avoid fatal "PHP Fatal error: Cannot redeclare[..]"

I would like to have the freedom of redeclaring particular functions in PHP. In another words, I have script that includes some files:
include_once(dirname(__FILE__) . '/../drupal/includes/bootstrap.inc');
include_once(dirname(__FILE__) . '/../drupal/includes/common.inc');
Later on in this script execution depending on some conditions, it is clear we need to load all application and not only particular files, however. This causes the error of redeclaration because when bootstrapping full App some of the functions were already included at beginning(i.e. bootstrap.inc, common.inc). What is solution? Can a remove an imported file before including it again to avoid redeclaration error? Can i have control on deciding when its okey to redeclare and when not in PHP?
Thank you for help
You can't "uninclude" a file. Once it's in, it's in.
Typically an include_once or require_once will do the trick. PHP will check to see if it's already included before trying to include it again. I would make sure that's being used in all locations. If the functions you're calling are inside a class, I would check out autoloading as a way to help manage your includes.

PHP - require_once seems to be including files multiple times and causing errors

In my PHP app, I have several calls to require_once. On my development PC this works fine and doesn't try to include the same file multiple times. However when I moved it to my production server I'm getting an error
cannot redeclare class myClass
After searching through the code I've found that this happens just after a call to require_once, so it must be that which is causing it.
I've searched through the entire project and this class is definitely only declared in one file, and it's only ever included through require_once. Is there some weird PHP config that would make require_once behave differently on the production server?
Thanks
Put the following at the top of your included class file:
if( class_exists( "<yourclassname>"))
{
throw new Exception("Class already defined");
}
You will now receive a clear error message where the class is re-called.
OK, this obnoxious problem can be caused by one of two things of which I'm aware:
symlinks; if loading from a file and a symlink pointing to the file
on OS X (and maybe Windows), the filenames are case-insensitive; PHP require_once is not
This had me confused for several hours today. I usually work in Linux, but I was forced onto a Mac where this becomes a problem.

Installing Zend Gdata Client library

Hi this is my first post on here. I am trying to install the Gdata Zend Client library without much success.
I have used these resources + scoured Stack Overflow.
https://developers.google.com/gdata/articles/php_client_lib
http://jeromejaglale.com/doc/php/google_calendar_api
I want to be able to add,edit etc events on google calendar via PHP. My problem/question is i really dont understand what the include_path settings are all about and how to set them in order to make the class work. Of course i checked php manual regarding this but still draw a blank.
I have downloaded the relevant class and uploaded it to my web root. In the past i would just include a class by using php include at the top of the page and this would suffice.
I am of the understanding that i need to change the php.ini file to show php where my class is. Does this mean that i have to put my class somewhere else other than the web root.
I am terribly confused about this step and i know that if i can get it installed, actually using the class should be relatively easy.
Thanks for any help.
Welcome! Your Q is about include files rather than ZF elements.
Understanding where the include setting can be made (and subsequently overridden) is an absolute key bit of information.
You need to find out where it is on the server you are working on.
echo ini_get('include_path');
Then dash off and really, really read the corresponding manual page.
Try out including a very simple file with an echo statement, and you will regain your sanity and confidence.
http://www.php.net/manual/en/function.include.php
Get that working then have a play with this:
http://www.php.net/manual/en/function.ini-set.php
The experience how you can include a file from the same directory (not generally a good idea if that is a public webpage - inside your webroot)
Then if you want to really chase this thing down, look at where you can set this in Apache and per-directory in .htaccess files.
Finally, you can just include a file by telling include/require the exact path from the top of the tree;
include /var/www/includes/libraries/and/so/on.php;
There are SO many places you can set and override this that you are really best off finding out where the server thinks the include directory is and putting your compoenents in there:
Now, when that comes to ZF stuff, I (on Deb and Ubuntu anyhow) put the contents of
Zend Framworks version XYZ ZendFramework/lib/Zend <-that folder into:
/usr/share/php/Zend <-into this place
Then setup your autoloader and Robert is your mothers brother...
Zend/Gdata.php line 124 is like this:
public static function import($uri, $client = null,$className=’Zend_Gdata_Feed’)
Change that to this:
public static function import($uri, $client = null,$className=’Zend_Gdata_Feed’, $useObjectMapping = true)

Including a php file in Joomla! 1.5 that calls another php file

I would like to include a PHP program into a Joomla! article, this program calls different PHP files that are used to display what I want, I have tried to install different Plugins such as Jumi, directPHP and others, but I keep getting the following error:
Application raised an exception class EDatabaseError with message 'Cannot connect to database server:mysql error: [0: Connection error to server '' with user ''] in CONNECT(, '', '**', )
'
The program runs fine Standalone, however it does not work when I'm running it on Joomla.
The connection parameters are obtained from an include "config.php" but it seems that they won't get the includes from the included PHP file.
Also when I try to include a menu I have made, which works standalone, redirects me to the index.php of Joomla! root dir.
Thanks.
I've done things similar to this, but have had to install a couple of extensions to get them to work.
First, i use the jce WYSIWYG.
then installed place anywhere (which lets you place modules inside articles)
create a new module, type=Custom HTML
code your php there...
I know this isn't exactly what you're describing, but it's the closest i've come in my experience.
If it doesn't work right away, be sure to check for the settings withing the JCE WYSIWYG so it's not breaking your php.
hope this helps!
Try to use 'Flexi Custom Code' extension is a good one
check it: http://extensions.joomla.org/extensions/core-enhancements/coding-a-scripts-integration/custom-code-in-modules/15251
Its a module extension which you can use with 'module anywhere' to place in the 'content area'(article). For calling different php file you can use the php include or required once code
Please feel free to ask if any doubts are there....implementing this

How to access a constant from within a class method

I'm migrating a php application from procedural to oop.
I use a DEBUG constant to activate errors and warnings output (in fact, I have thee, every one makes the output more verbose.
But I can't find a way to access those constants from within a method.
The constants are defined before autoload in a separate file.
In the utility file I have
define('DEBUG', TRUE);
And inside a given method I tried to
if(!defined('DEBUG')) define('DEBUG', FALSE);
But I always end up with DEBUG=FALSE.
What am I doing wrong? I'm a total noob to oop, so be gentle please :-)
Clarification
Every class has his own file.
In any given script, the first thing I do is to include the utility file. The utility file is the one who defines DEBUG and has the _autoload function.
script_file.php
includes utility_file.php
defines DEBUG
has _autoload function
according to this, you should access DEBUG (no prepending $) in your code directly. are you including or requiring your utility file in the same file that has the function you're talking about? i don't think this is an OOP problem
darkphoenix was right, This wasn't an OOP problem. This was a NetBeans problem.
I'm using NetBeand and uploading the files to a remote server upon save. I've set the DEBUG constant to TRUE in the utility file and hit save on NetBeans, the save process (apparently) went without problems (no warnings or anything).
Big was my surprise when latter I logged in via SSH did a cat on the file. The file was never saved to the server. My local copy has my last edit, but the remote one doesn't...
Moral of the story: I hate you NetBeans

Categories