-2

I just started learning PHP from a book. In the OOP chapter there is an explanation of the __autoload() function for "Automatically Load Class Files" but my book doesn't say where I should declare this function.

I tried to Google it and in the PHP documentation but I really can't find where I should declare this function. In "global scope" (I don't know if it is the same as JavaScript)? Inside the class that should be autoloaded? Or in the "local scope" of the class where I have to load the class?

halfer
  • 19,824
  • 17
  • 99
  • 186
Carl
  • 33
  • 9
  • There are several tutorials on the net. One is [link](http://php.net/manual/en/language.oop5.autoload.php) – B001ᛦ May 28 '15 at 10:19
  • 1
    It is incredible how a downvoter can read the entire question in about 2 seconds and leave a don vote without explaining why, thank you downvoter. (I posted this as doon as I saw the donvote but I had a latency_period) – Carl May 28 '15 at 10:22
  • 1
    If it would have been only one it would have been possible the downvoter is an idiot. Once it starts to hit -4 you should rather look at your own question instead. Your text is full of spelling mistakes, it's hard to read. Most information can be found when doing proper research. You said you did research using the manual for example which is odd because it clearly states you should look into using `spl_autoload_register` instead. So from the looks of it you really didn't spend much effort neither in your question nor in your research. – PeeHaa May 28 '15 at 10:27
  • @Carl Have you read the rules of SO? People dont want to do your homework ;) – B001ᛦ May 28 '15 at 10:30
  • Usually an autoloader is set up in an initialisation routine - one of the first things to be called before your app handles a web request. See the "Related" sidebar here `---->` for lots of examples. – halfer May 28 '15 at 10:32
  • Thanks @bub, but i cant understand how you have concluded that it is a homework, well it is not. – Carl May 28 '15 at 10:41
  • Thank you @halfer for your useful help (differently of the others) – Carl May 28 '15 at 10:43
  • The fact is that you really did'nt make a deep research my friend ;) – B001ᛦ May 28 '15 at 10:49
  • Hi @Carl and welcome on StackOverflow, please understand that this site is full of people (about 4.3M), unfortunately there is someone which puts donw/up votes without reading the questions/answers just to earn [badges](http://stackoverflow.com/help/badges) but the most of us just want to make this site better. Your question seems poor and has a lot of answers even outside this site. Think about this site like the first (or one of the first) resource to have an answer but the [Ask question](http://stackoverflow.com/questions/ask) button as the last resource. – Cliff Burton May 28 '15 at 11:00

1 Answers1

3

The way of using the __autoload() function is :

<?php

    function __autoload($class){
        if(file_exists($class . ".php")){
            require_once $class . ".php";
        }
    }


    $class1 = new Class1();
    $class2 = new Class2();

On the very top of your page declare the function than you can start using it just like in the example below .

If you want it to be available site-wide than consider making a new file type the code there and include that file in the top of your page.

something.php

<?php

    function __autoload($class){
        if(file_exists($class . ".php")){
            require_once $class . ".php";
        }
    }

Than just include something.php on all the pages you need like :

<?php

    require_once 'something.php';

    $class1 = new Class1();
    $class2 = new Class2();
Paul Dumitru
  • 68
  • 1
  • 10
  • 1
    `spl_autoload_register` tends to be better, since you can have several active at the same time. But for the OP, this is probably a good start! – halfer May 28 '15 at 10:46
  • Thanks for your help, @Paul Dumitru – Carl May 28 '15 at 10:50
  • Your welcome, Carl. This is the most basic example , you will have to do some research yourself in order to find out more . You can find tons on good examples and many informations right on the php.net website . – Paul Dumitru May 28 '15 at 17:55