Solved

spl_autoload_call.  Where is it

Posted on 2014-03-31
3
601 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 35

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 110

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

634 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