Solved

spl_autoload_call.  Where is it

Posted on 2014-03-31
3
554 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
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
when gonzo class not there, look in folder
and two programmers adding files which become autoloaded

Thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now