Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

spl_autoload_call.  Where is it

Posted on 2014-03-31
3
Medium Priority
?
636 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 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 1000 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 111

Accepted Solution

by:
Ray Paseur earned 1000 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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

916 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