Jump to content

Recommended Posts

Hi.

 

I am trying to learn to autoload classes, but im having some problems understanding the implementation and the usefulness of this.

 

 

index.php



$classname = 'class.validator';
 
function __autoload($classname) {
$filename = $classname . '.php';
require_once($filename);
}
 
$validator = new Validator;
 

class.validator.php



<?php
 
class Validator {
 
function validateSomething() {
...
}
 
}
?>

This code does not work even though i followed a tutorial. And even if it would work, then i fail to see how this is better than:



<?php
require_once('class.validator.php');
 
$validator = new Validator;
 
?>
Edited by Pain
Link to comment
https://forums.phpfreaks.com/topic/278440-__autoload/
Share on other sites

I have a utilities file called oddly enough utilities.inc.php - here's a portion of it:

// Autoload classes from "classes" directory:
function class_loader($class) {
    require('classes/' . $class . '.php');
}
spl_autoload_register('class_loader');

Then in the classes folder I have a file called Registration.php - here's a portion of that one:

<?php
class Registration extends DBConnect {
   
     // The member attributes containing required and optional information.
    // The attributes must correspond to the database table columns:
    
    private $id = NULL;
    private $userType=NULL; // Required (assigned)
    private $username=NULL; // Required
    private $email=NULL; // Required   
    private $pass=NULL; // Required
    private $fullName=NULL;
    private $address=NULL;
    private $city=NULL;
    private $state=NULL;
    private $zipCode=NULL;

I finally I have a file in my root directory called register.php....here's a portion:

// Need the utilities file:
require('includes/utilities.inc.php');

// ...... more code in between.......

// Process form once user clicks on Register:
if (isset($_POST['action']) && $_POST['action'] == 'register') {
    
     $guest = new Registration();    // Registration Class extends DBConnect Class.            
      $errorMsg = NULL;
    
     $data['userType']  = 'public'; // User has no rights to add or edit pages.
     $data['username']  = trim($_POST['username']); // Required
     $data['email']     = trim($_POST['email']); // Required
     $password          = trim($_POST['pass']);
     $passOK            = $guest->isPassWordStrong($password);

As you can see I don't have to worry about loading that class or any other class just as long as I put them in the classes library, I imagine if you were working for a large company you would even have sub-directories thus each employee can do their own thing without goofing up anyone's else code. ;D

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/278440-__autoload/#findComment-1432558
Share on other sites

$classname = 'class.validator';
 
function __autoload($classname) {
    $filename = $classname . '.php';
    require_once($filename);
}
 
$validator = new Validator;
That's not how autoloading works. The idea is what when your code tries to use a class that PHP has not loaded yet, the autoloader function provides the opportunity for your script to load the correct file before PHP finally gives up and barfs out an error.

 

In your script, upon reaching the "new Validator" part, PHP knows that the class has not been loaded yet. You have defined an autoloader so it gets called with the class name: effectively calling __autoload("Validator").

 

Looking inside your function, hopefully you can see that $filename will become Validator.php, which is not the correct file name to be loading (you wanted class.validator.php).

 

You probably want to make the first line of the function be like $filename = "class." . strtolower($classname) . ".php";

Link to comment
https://forums.phpfreaks.com/topic/278440-__autoload/#findComment-1432562
Share on other sites

Thanks for the help guys. The thing i did not understand here was this line:

function __autoload($classname) {

I didn't get how does the PHP now what the $classname is. I found a tutorial which explained that instantiating a class tells the __autoload which class is an argument.

$validator = new Validator;

am i correct?

Edited by Pain
Link to comment
https://forums.phpfreaks.com/topic/278440-__autoload/#findComment-1432567
Share on other sites

It's a magic function, PHP just knows. It's magic. :-P

 

I found a tutorial which explained that instantiating a class tells the __autoload which class is an argument.

.

 

Like that.

 

 

And the reason it's better is once you have more than one file to include, you have now reduced your code significantly.

Edited by Jessica
Link to comment
https://forums.phpfreaks.com/topic/278440-__autoload/#findComment-1432571
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.