Solved

spl_autoload_call.  Where is it

Posted on 2014-03-31
3
576 Views
Last Modified: 2014-04-01
from larry ulman php oop book
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Autoloading</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<?php # Script 8.10 - autoload.php
// This page uses the ShapeFactory class (Script 7.2).

// Create the autoloader:
function class_loader($class) {
    require('auto/'.$class . '.php');
}
spl_autoload_register('class_loader');
$_GET['shape']='rectangle';
$_GET['dimensions']=array(9,5,8);
// Minimal validation:
if (isset($_GET['shape'], $_GET['dimensions'])) {
    
    // Create the new object:
    $obj = ShapeFactory::Create($_GET['shape'], $_GET['dimensions']);
    
    // Print a little introduction:
    echo "<h2>Creating a {$_GET['shape']}...</h2>";

    // Print the area:
    echo '<p>The area is ' . $obj->getArea() . '</p>';

    // Print the perimeter:
    echo '<p>The perimeter is ' . $obj->getPerimeter() . '</p>';
    
} else {
    echo '<p class="error">Please provide a shape type and size.</p>';
}

// Delete the object:
unset($obj);

?>
</body>
</html>

Open in new window


where is internal function spl_autoload_call()
circled in call stack of picture
where is spl_autoload_call
0
Comment
Question by:rgb192
3 Comments
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 250 total points
ID: 39968017
When you try to use a class that has not yet been loaded into PHP's memory, then PHP would normally just throw an error. For example, let's say I have a class called Gonzo defined in a file called classes.php. Then I have a PHP script called program.php which tries  to do:

<?php
$x = new Gonzo();
?>

Since Gonzo is in classes.php, PHP has no idea what Gonzo is, so it would try to throw an error. However, the spl_autoload_ functions will allow you to tell PHP, "When you can't find the class I'm asking for, this is how to find the right file that loads the class."

The spl_autoload_call function is there to try and look for the class.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39969134
The autoload magic happens here:

function class_loader($class) {
    require('auto/'.$class . '.php');
}
spl_autoload_register('class_loader');

Open in new window

SPL_Autoload_Register() tells PHP to use the function named class_loader() for missing classes.  Class_Loader() takes the class name as its input argument, adds the suffix '.php' to the end and the directory 'auto/' to the front.  Then it calls require() to bring in the script.

Why do this?  Well, maybe you're working on one part of the project and a colleague is working on another part of the project.  Your script depends on your colleague's script in a way like the example above, where the example script depends on the ShapeFactory class.  You and your colleague need to test a number of incremental changes on both sides of the project.  Rather than try to coordinate your test schedules by telephone, you can just use the autoloader and it will pick up the most recently deployed version of the ShapeFactory (or similar) script.

This idea, while it seems handy, is getting some push-back.
http://fabien.potencier.org/article/11/what-is-dependency-injection

See also: https://getcomposer.org/doc/00-intro.md
0
 

Author Closing Comment

by:rgb192
ID: 39970786
when gonzo class not there, look in folder
and two programmers adding files which become autoloaded

Thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question