Solved

spl_autoload_call.  Where is it

Posted on 2014-03-31
3
591 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

752 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