dynamically load PHP code from external file - php

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.

Related

How to include a specific function using require_once() or include() functions in PHP?

How to include a specific function based on requirement from a php file containing various function definitions.
eg. PHP file functions.php contains 3 functions a(), b() and c()
I have to use require_once('/f/functions.php') in other.php file to use function c.
What could i do to only include the function c in file other.php file using require_once() function and avoid other functions not to be included ?
There is no built in way to do it. Although there are ways to go around it if you do it for the sport.
While this is highly not recommended. Really, just dont do it... here's how:
1) Read the file as text.
2) find the function block and extract it
3) run it through eval.
This will cause any global state set up in the file to not be run. Which you may or may not want.
SuperClosure does something similar I believe to serialize closures, which by default php does not support.
One could leverage the PHP7 abstract syntax tree to also includes related functions. For example, if there are three functions defined - A, B and C. A uses B internally. If you include A, you would want to also include B.
This wont be more performant than just including the whole file, but feel free to prove me wrong with benchmarks.
You might be interested in namespaces. Both classes and functions support it.
You cannot, period. PHP doesn't have any sort of module system. To "include functions", all you can do is execute a source code file (what include will do), which will by the act of executing the code in the file define those additional functions, or anything else that's in the file. You cannot selectively pick only certain parts of the code to run.

php re-evaluate included files

I read about the class autoloading in PHP, but till now I didn't understand why we should use __autoload() method?
I read that
PHP doesn't use
this method becuase it has the handy little include functions,
include_once and require_once, that prevent you from loading the same
file more than once, but unlike a compiled language, PHP re-evaluates
these expressions over and over during the evaluation period each time
a file containing one or more of these expressions is loaded into the
runtime in this site why we should use autoloading,
but I don't understand what is the meaning of PHP re-evaluates in the above statement!!
why require-once don't solve the problem of loading php file more than once?
The original article is more clear when you read it more widely (see below) :
It simply says that __autoload() is smarter than include_once() because the function include_once() has to be coded explicitly when the class may be required, and also because this function needs to be processed each time it appears in order to know if the file given in argument is already loaded or not.
The other function __autoload(), on the contrary, can be called only once for several classes you may need. And then PHP tryes to load the corresponding source file only when a class definition is missing.
We can sum up this argumentation by saying: you need one include_once() for each Class/Function source, while only one __autoload() may be enough for a set of Class source having the same location rule.
Snippet of the article:
Why you should use an autoload function in PHP
The loading of classes is something that managed languages like Java
and C# don't need to worry about, class loaders are built into the
compiler.
[...]
PHP doesn't use this method becuase it has the handy little include functions, include_once and require_once, that prevent you
from loading the same file more than once, but unlike a compiled
language, PHP re-evaluates these expressions over and over during the
evaluation period each time a file containing one or more of these
expressions is loaded into the runtime. That is where the Standard PHP
Library (SPL), introduced in PHP 5, and the wonderful little _autoload
function come in to enhance the speed and uniformity of your PHP code.
__autoload is a magic function, that you define, that enables PHP to let you know when it doesn't have a class loaded, but that class
needs to be loaded.
The include_once statement itself is reevaluated whenever encountered.
for ($i = 0; $i < 100; $i++) {
include_once 'foo.php';
new Foo;
}
This will evaluate ("run") the include_once 100 times. That can be something of a slowdown. On the other hand:
for ($i = 0; $i < 100; $i++) {
new Foo;
}
When using autoloading, the logic for file inclusion will only be triggered once, the first time the class is needed.
but till now I didn't understand why we should use it
When you have structured and organized your work, because of the organization it results that you have many similar class files. The simpler example is an mvc, but not only, any custom structure will result in similar files containing a class, then because of the similarity you put them in the same folder and also you use a common file naming convention for example you can have a controller and a model directory.. check example file: employeeModel.php, statisticsModel.php, indexController.php, errorController.php
Well you can take advantage of this fact, check this customized autoload function:
/*** function to include model and view classes ***/
function __autoload($class_name)
{if(__DEBUG) eval(__DEBUG_EVAL);
/*** Load a model class ***/
$mfile=__SITE_PATH .'model/'.$class_name.'.php';//echo 'model file'.NL;v($file);
if (file_exists($mfile)){
include ($mfile);
if(__DEBUG) //store debug info before include
eval('error_log("autoload Success file exists: ".$mfile.NL);');
return true;
}
/*** Load a view class ***/
$cfile=__SITE_PATH .'view/'.'/'.substr($class_name,0,-4).'/'.$class_name.'.php';//v($file);
if (file_exists($cfile)){
include ($cfile);
if(__DEBUG) //store debug info before include
eval('error_log("autoload Success file exists: ".$cfile.NL);');
return true;
}
return false;
}
It also has a few lines for debugging that can easily be removed later. Because of similarity in things it can decide it self what to include and also to report errors when occur. Without this autoload function you would have to care that class files are availoable before use. Also this function will allow to include a file once, if you check carefully it does not use include_once, this means that autoload fires only when the file has not been seen before, in contrary to simple file inclusion which is fired every time the code is executed as very correctly decese notice it.
Conclusion Autload = fires once per file, automates things, so you execute a class directly without caring to include it.
Autoloading means if you need some classes to be included automatically in the scripts like
require_once ("class.user.php");
require_once ("class.module.php");
To avoid such code for each script you can use Autoloading functionality of php

Where to place include statements in a PHP class file

Where is it wisest to include files in a PHP class file? For example if one of the methods needs a external class, should I include the file where it is used in that method, or should it be done before the class? Or in the constructor? Or? What do you recommend? Pros? Cons? Or is it just a matter of taste really?
include_once 'bar.class.php';
class Foo
{
public static function DoIt()
{
new Bar();
}
}
vs
class Foo
{
public static function DoIt()
{
include_once 'bar.class.php';
new Bar();
}
}
I prefer it on top, the same convention as for #import/import/using in c/java/c# as it immediately lets you know what other classes your class is depending on.
You may also want to check out require_once() instead of include_once() as it will halt with an error instead of giving a warning when the included file contains an error. But of course that depends on what kind of file you're including and how critical you deem it to be.
I would say that it depends.
So, if there is a rather large code base, and you would prefer to keep the code loaded into memory each time there is a page request down to a minimum, then I would suggest only including the other php file when needed.
However, if that script is always needed, then include it at the top of the script.
It really comes down to the situation and requirements.
Hope that helps.
It depends on architecture you are using. Including files in the beginning is neat, but if that file prints text then you wont be able to manipulate headers etc.
When you are using MVC pattern controller should include class files.
If you're sure you need the file included, do it at the top. If you need files included on demand, you might want to look into spl_autoload_register() to ease the pain.
It is always good to include external files on top of the page. It will be very easy to locate later. If the included file is very large then include it wherever you need. See also the documentation for require_once and include_once.
There's also simply require and include. Know the difference between them and which to use when.

Does including classes that you don't use have impact on performance?

like
require "class.a.php";
require "class.b.php";
require "class.c.php";
class main{
function main(){
if(condition_is_met(){
$this->something = new A();
}else{
$this->something = new B();
}
}
}
Should the files be included in the condition check with require_once, and not all the time?
The question is not clear. In the current code, I think all of the file(s) will get included, whether you use (declare variable of these classes) them or not. If you wan't to not load the class(es) you will not use, you can use the __autoload() function.
http://php.net/manual/en/language.oop5.autoload.php
PHP has to open the file and parse it so it has some impact. For a few files I wouldn't worry about it but it can get out of hand as your files increase. That's why there's autoload, which allows you to load class files only when needed, without having a long list of requires at the top of your files:
http://php.net/manual/en/language.oop5.autoload.php
Also take a look at spl_autoload_register:
http://www.php.net/manual/en/function.spl-autoload-register.php
The only performance it should effect is the time to parse it, but I think that is preferred over complicated include logic hidden midway inside of your file. Not to mention that if you put the require inside of the if statement it is like you inserted that file's text inside of that if statement, which isn't right (and may not work).
Can anyone tell me if you can declare a class inside of a function/if statement?
Anytime you use include or require, PHP is basically copy/pasting the code from the required file into your code. So no matter where you put it, PHP is still opening the file, reading it and dropping it in there, it won't be affected by an if block. In other words, require is parsed before the code is actually run, so yes, you will take a (very small) performance hit even if require is put in an if block and never run. Keep in mind, this is a very small impact. Lastly if you are worried about it, I would use require_once - this ensures that this parsing does not happen twice, for example if a second required file requires the first file, this redundancy won't amount to a second performance hit.

How does PHP see classes - only in include files?

In PHP are classes only seen when they are in an include file? In Java I can see them in another file without including that file in my current file. In PHP is the only way to see any given class to include it in your file? So I'm just including my class file(s) everywhere?
In PHP are classes only seen when they are in an include file?
Yes. However, in PHP 5, there is the new Autoloading feature that allows you to build a function that includes a file when a class name is invoked. That effectively makes it possible to auto-initialize classes.
The simple example in the manual (I extended it slightly) makes it clear how this works:
<?php
function __autoload($class_name) {
require_once $class_name . '.php';
}
$obj = new MyClass1(); // Autoloader will load "MyClass1.php"
$obj2 = new MyClass2(); // Autoloader will load "MyClass2.php"
?>
Advanced autoloaders like Zend Framework's Zend_Loader_Autoloader (and the Standard PHP Library's spl_autoload_register(), cheers #ircmaxell) make it even possible to add different autoloading rules for different prefixes, allowing for libraries to be loaded from varying directories with varying naming conventions.
Generally speaking, yes. However, do note that includes are cascaded--in the sense that: if you include file a.php which includes file b.php, you can now see file b.php in your current file.
Also, PHP 5 offers Autoloading Classes which I recommend you take a look at:
http://php.net/manual/en/language.oop5.autoload.php
Yes -- PHP doesn't have any visibility into code which wasn't included directly in the file using the code.
However, instead of including (or the preferred method -- requiring) .php files everywhere, many developers choose to use the PHP autoload ability (http://php.net/manual/en/language.oop5.autoload.php) to automatically load the required file when you use a particular class.
Yes, for a class in a file to be available, that file has to be included.
Including a file can be done in a number of ways, either using the following functions:
include(filename);
include_once(filename) - which only includes the file if it's not already loaded
require(filename) - equal to include, except that the script will halt if file is not available
requier_once(filename)
Files can also be autoloaded through the __autoload or spl_autoload_register functions. More info on autoloading classes on PHP.net.
Yes only in include statements will you have access to the functions in other php files. PHP is by standard a Procedural language. It works from the top order down, so if you wish to perform class like includes look at Object Oriented PHP or include the files at the top of the php page.

Categories