I'm using methods from a PHP class all over my code and I don't want to do "require_once" in every file I'm using that class. Is there a way to include the class in a single file, and then access it from everywhere in the code?
Thanks!
here ya go
http://php.net/manual/en/language.oop5.autoload.php
You could potentially use the auto_prepend_file directive (http://php.net/manual/en/ini.core.php), which you should be able to set via .htaccess if you have no other method of doing so.
Related
I'm always using a function to write to a log file, but this function is defined in a file among many other things that I don't need to include.
I was wondering, is it possible to define a function somewhere inside php to make it available without the need to include the source file? Sort of like how I can just use echo or die, or isset. Could I create my own function to use it this way?
Thank you.
No. To do that, you'll have to write a PHP extension in C. Any PHP code will always need to be included explicitly one way or another.
PHP has the option to always automatically include a file at the beginning though: http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file
Actually, you need to make a module with your function.
Other ways:
make autoload. http://www.php.net/manual/en/language.oop5.autoload.php
put only this function to other file and include it everytime you need.
you can add you log class in set_include_path path or add this function to pear library class
I'm modifying a website and I would like to add a config file for it so that when when moving the website to another server, I only need to change environment parameters like database information only once. But I don't know how to apply the config file for the whole site. I tried to use include_once() and make it global using keyword global but it only works in the current file. If I don't use define() to make these parameters as constant, are there any other way to achieve this? Thank you.
You can create a class Config and store your config parameters as Class Constants. Then include this class everywhere you need it, or use Autoload it.
class Config {
const databaseUsername = "user123";
const databasePassword = "pass456";
}
echo Config::databaseUsername;
echo Config::databasePassword;
But as in my experience, define() works perfectly well, unless you need to store more complicated data types (arrays for example).
To avoid include the config file everywhere, you could set auto_prepend_file in your PHP.ini, but I personally find this solution obscure, since it's not obvious for another developer working on your code that a PHP file is auto-included.
I'm somewhat new to PHP. I wrote static class that I would like to use and call, however when I try to call it I get a fatal because the php class file is not in the same directory as the calling file. Do I have to include it in order to use it?
Yes. Use include to include other PHP files. Even if your class file in the same directory, you still need to include it (you can also include files from other directories, obviously).
Yes. Any code that is in another file must be included. You can avoid explicitly loading the class in that page by using require() if you use an autoloader.
Yes. The class must be loaded to use it. This is true for instantiting it or calling a static function. Use require, require_once, include, or include_once to load the class file.
I started to use the PHP __autoload function and now I'm getting those weird Fatal error: Cannot redeclare class xxx errors.
It's weird since these errors occur on classes I don't even load with the autoload function. And I also use require_once to include the files.
I'm really puzzled about this. Anyone knows anything about this kind of errors when using autoload?
require_once/include_once only looks at the file name when they're trying to include a file, not a class name. So you can have class Foo in both Foo.php and B.php, and then you'll get that error.
I'm not sure how __autoload would cause you any problems, unless __autoload requires Foo.php because it needs class Foo, and you require B.php manually which redefines class Foo.
By the way, use spl_autoload_register instead of __autoload.
i had same situation .. all i changed was and its working for me
include('xxx.php');
to
require_once('xxx.php');
I am not pretend on the best answer. Just my thoughts.
__autoload() is deprecated in PHP 7.2 and will be possiblly deleted.
I found this way to upload/include files
//initialize a function which will upload files
function upload(){//you can call the function as you like
//your path to files goes here
}
spl_autoload_register('upload')//specify the function, where you include your files(in this case it's upload function
I've had this before, not sure what caused it though. Make sure you're using require_once/include_once rather than the normal versions for starters. I think the problem was related to using class_exists without telling it to skip autoloading (class_exists($name, false)) - are you doing this?
I found the problem. Apparently when it wanted to include Bank class, it took the "bank.php" file from the current directory ^^. Which is just the bank page, it should have include "src/model/Bank.php". I removed the current directory from the search list and it has been fixed.
So always make sure the autoload function is including the correct files. Good practice is too name your files more straightforward like class.Bank.php or class.User.php instead of Bank.php or User.php.
Thanks for your help and quick response anyway!
Code is in a static class in an external file eg. /home/test/public_html/fg2/templatecode/RecordMOD/photoslide.mod
How do I load this into my script on demand, and be able to call its functions?
As long as the included code is wrapped in PHP blocks, you can use include or require for this.
Like so:
include( '/home/test/public_html/fg2/templatecode/RecordMOD/photoslide.mod' );
You can then do whatever you wish, call functions, etc.
To use the variables or classes (static or otherwise) they need to be loaded before being used. Typically you would make a call like:
<?php
require('/home/test/public_html/fg2/templatecode/RecordMOD/photoslide.mod');
?>
You can also do without the parentheses:
<?php
require '/home/test/public_html/fg2/templatecode/RecordMOD/photoslide.mod';
?>
...somewhere at the top of your code.
It would be good to review include(), require(), include_once(), and require_once()
Might be too advanced right now, but PHP supports an autoloader. You would still use the include/require code that is mentioned above. But instead that code would live inside a special function that will be called anytime you access a class/interface that hasn't already been loaded. This will allow you to see what is being requested and dynamically load files on demand.
Including/requiring a few files is fine. Once you get into a large site with tons of files it will be easier to use the autoloader then explicitly writing an include/require line for each. Plus you will save memory by not loading things you aren't using.
Autoloader Docs, thats the easy version. The better implimentation is with spl_autoload_register.