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

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.

Related

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.

How to include external PHP classes in Joomla

I have few PHP classes and files that I want to include in Joomla so that I can call those classes. I tried doing require_once config.php to include a PHP file which also includes all the classes that I would want to use in Joomla.
But each time I execute the page I receive the error below:
Fatal error: Class 'SomeClassName' not found
is there any other way to include external PHP classes or files in Joomla?
Thanks in advance!
Please use Joomla autoloader. Is better.
<?php
// Register an adhoc class.
JLoader::register('AdhocClass', '/the/path/adhoc.php');
// Register a custom class to override as core class.
// This must be done before the core class is loaded.
JLoader::register('JDatabase', '/custom/path/database_driver.php', true);
Edit:
Load classes with autoload instead of require/include have a better performance, because PHP will only read (require access to the disk) and compile (require memory and CPU usage) if you really use your class.
To do the same with require/include you have to be sure to only use if will really use the class.
Source:
http://developer.joomla.org/manual/ch01s04.html
require_once should work just fine within Joomla. Make sure the class you want to use is really loaded within your file, and the file is properly referenced in the require_once. Something is going wrong there and it has nothing to do with Joomla itself :-)

PHP's "use" keyword not properly including

I am currently refactoring a project which has been halfheartedly ported to Yii. There are some classes in the components folder which are included in a controller with the PHP "use"-keyword. This gives me a "include(protected/components/classes/SClass.php): failed to open stream: No such file or directory" error though.
What's really strange about it is, that changing the name (used by "use") to a non-existent file gives me a fatal error. Any ideas?
The use keyword by itself in PHP does not do any including of other files. It merely tells PHP that the namespace defined in the use statement may be referenced by code further down in the current PHP file.
However, what is likely happening here is that your system has an autoload function defined. If there is an autoload function, PHP will call this function whenever it encounters a class name that it doesn't recognise. The autoload function searches for the class file to load and includes it if it can find it. This is probably where your errors are occurring.
In the first case, this is the sequence of events:
the use statement is referencing a valid namespace, but this is ignored until a class in that namespace is referenced in the code.
When the class is referenced, PHP says "I don't know this class yet, lets autoload it".
The autoloader function is run, and builds a path to include. This usually comes from the namespace and classname of the class, but can be whatever the autoload function is written to expect.
In this case, it sounds like the autoloader is building the path and running include() on that path, but the class doesn't exist where the autoloader expects. Hence the "file not found" error.
In the second case, where you change the use statement:
The use statement now references a different namespace, but you probably haven't changed the code later in the program that actually references the class.
The program reaches the code that calls the class, but it doesn't recognise the namespace because it doesn't match the use statement any more, so instant fatal error.
You might want to check the file ownership and permissions, and check for PHP security as is mentioned HERE

What is the purpose of using `include` in 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.

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