Pain Posted May 27, 2013 Share Posted May 27, 2013 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; ?> Link to comment https://forums.phpfreaks.com/topic/278440-__autoload/ Share on other sites More sharing options...
Strider64 Posted May 27, 2013 Share Posted May 27, 2013 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. Link to comment https://forums.phpfreaks.com/topic/278440-__autoload/#findComment-1432558 Share on other sites More sharing options...
salathe Posted May 27, 2013 Share Posted May 27, 2013 $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 More sharing options...
Pain Posted May 27, 2013 Author Share Posted May 27, 2013 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? Link to comment https://forums.phpfreaks.com/topic/278440-__autoload/#findComment-1432567 Share on other sites More sharing options...
Jessica Posted May 27, 2013 Share Posted May 27, 2013 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. Link to comment https://forums.phpfreaks.com/topic/278440-__autoload/#findComment-1432571 Share on other sites More sharing options...
ignace Posted May 27, 2013 Share Posted May 27, 2013 Please consider using spl_autoload_register instead of using __autoload. Link to comment https://forums.phpfreaks.com/topic/278440-__autoload/#findComment-1432578 Share on other sites More sharing options...
Pain Posted May 27, 2013 Author Share Posted May 27, 2013 Thanks! Everything's magically clear to me now! Link to comment https://forums.phpfreaks.com/topic/278440-__autoload/#findComment-1432585 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.