0

Am I right to assume that autoload is a performance issue rather than making my life easier as a developer? I mean if I put all my classes in one include, and I don't have any performance issues, should I be worried about autoloading at this early stage of my learning curve?

Thanks for any light you would care to shed on this.

Edited by PerplexedB: typo

4
Contributors
12
Replies
60
Views
3 Years
Discussion Span
Last Post by PerplexedB
Featured Replies
  • 1

    Perhaps [this article](http://www.sitepoint.com/autoloading-and-the-psr-0-standard/) may help you. Read More

0

You mean if you include all your classes at once? Of course, that does impact your performance in a way, but it will probably not be significantly in small projects. A question though: why would you include classes that you don't need on every page?

0

Thank you very much for caring. Well I thought that would just be easier. I thought I'd register (include really) all my classes as I develop them in one includes.php, and include that in each "view" page I develop.

I thought that that was how that was "done". Please set me straight if it is not.

1

It is indeed done like that, in general, for as far as my knowledge goes :). However, if you for example have a Registration class, that helps members register an account for your website, I wouldn't include it on pages that only registered people can see, personally, as it wouldn't have any use if it only helps non-registered members register an account.

0

Yes the __autoload() function is a useful one, but I myself don't like to rely solely on that. The manual itself specifies:

By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

But playing around with it and getting to know how it works can be useful. Maybe you like it more than I do ^^.

0

pritaeas, minitauros, thank you, I had read the page in the manual, and it left me somewhat confused.

Not sure I uderstand where you code that __autoload function. Or is it a method in a parent class, in an interface? And what exactly is it doing, when is it invoked, and by what.

And then they tell you that you should actually be using spl_autoload. When I grow up I will go through this again, and stick with my include.php for the time being.

Another thing that I find strange is that you autoload the class by its name, so how do you manage paths. Surely I'm not the only one who wants to organize his classes in multiple folders.

Edited by PerplexedB

0

Well I haven't used spl_autoload ever, but for as far as I can see when I read the manual, it's worth checking it out. An example of my use of the __autoload() function (dates from over 2 years back, so it's just an example, not sure about the current standards):

function __autoload($class_name)
{
    if(file_exists('includes/classes/' . $class_name . '.class.php'))
        include_once 'includes/classes/' . $class_name . '.class.php';
    else
        return false;
}

This function is defined in your includes.php, for example, so that it is defined in every document that is loaded. This worked for me back then, but again, if you want to use it I would suggest you do some more research to be sure if this is still up to standards =).

Edited by minitauros

0

Not at this moment. But if you're referring to my ORM thread, I'm leaning towards using the PSR-0 version.

1

I honestly believe, you can use composer autoloader and symfony class loader and http-foundation.

Supposed we have our development environment in localhost/myapp/, and we are running xampp or wampp on windows. We can easily solve the autoload problems in two ways. This is just an example. If you will running this on linux, then just add $ in front of the composer command that simple

First install the composer..,
1. Test the composer after installation and make it is working. Open the command prompt, and then type

composer

it should return the version and info. about your composer version.

  1. Now let us create our simple application using composer autoload. Create the directory called myapp in your xampp/htdocs/myapp. Inside the myapp directory create another directory called application.

  2. Open your text editor or any suitable source code editor and then paste codes below, and save as composer.json

    {
    
    "require": {
        "symfony/class-loader": "2.4.*@dev",
        "symfony/http-foundation": "2.3.*@dev",
        "phpunit/phpunit": "3.7.*",
        "swiftmailer/swiftmailer": ">=4.2.0,<4.3-dev"
    },
    
    "minimum-stability": "dev"
    }
    

I added some common vendors found on PHP MVC framework like Laravel, PPI and Kohana. I mean this is an actual build if you want to create your own framework.

I also added phpunit for you to experiment. If you will be applying for a 6 figure job as a developer, you must know all this, otherwise you will be debugging other people's codes for a long long time.

  1. Go back to your command prompt and type

    cd c:/xampp/htdocs/myapp
    

once you are in the myapp directory type

    composer install

hit enter..

  1. Now, if you look at our myapp directory there should be a new directory there called vendor. This is created to meet the PSR-0 (PHP standard recommendation in MVC framework). Symfony and Laravel follow this recommendation down to its bone. The myapp directory should now look like this

    myapp
        vendor
            bin
            composer
            phpunit
            swifmailer
            symfony
            autoload.php
    composer.json        
    
  2. Inside the vendor directory add your own classes directory called myclass. The updated myapp directory should now look something like this

    myapp
        vendor
            bin
            composer
            phpunit
            myclass
            swifmailer
            symfony
            autoload.php
    composer.json        
    
  3. Create a new php document called helloclass.php and paste the follwing codes below

    namespace myclass\helloClass;
    
    class helloClass{
    
    public function __construct(){}
    
    public function saySomething(){
    
    return 'Hello World! from myClass';
    
    }
    }
    

    save helloclass.php in the myclass directory.

    1. Update your composer.jason.. the codes in it should now look like this

      {

      "require": {
      "symfony/class-loader": "2.4.@dev",
      "symfony/http-foundation": "2.3.
      @dev",
      "phpunit/phpunit": "3.7.*",
      "swiftmailer/swiftmailer": ">=4.2.0,<4.3-dev"
      },

      "autoload":{
      "classmap":[
      "vendor/myclass"
      ]

      },
      "minimum-stability": "dev"

      }

  4. Save the changes on the composer.json and then go back to your command prompt, and then type

    composer update

a new composer.lock should be created for you.

  1. Let us create our very application based on the autoloader and symfony class loader. Our helloclass.php above should be loaded via composer autoloader.php and the composer class loader. Create a new php file called index.php and save this inside the myapp directory.

    <?php 
    
    
        echo 'This is my first app';
    

direct your browser to this page and make sure it is working.

  1. Let us modify our codes above and make our class auto load meeting the PSR-0 recommended standard.. change the index.php codes to this

    <?php
    
        ## this requires the composer autoload
        require_once __DIR__.'/vendor/autoload.php';
    
        ## these requires the symfony modules, loader is just a placeholder
        $loader = new Symfony\Component\ClassLoader\UniversalClassLoader();
        $object = new myclass\helloClass\helloClass;
    
        echo $object->saySomething();
    
  2. Save changes, and direct your browser to localhost/myapp/, and you should see " Hello World! from myClass ".

that should conclude my mini tutorial. If I will ever have a longer attention span the next time, I will write a much longer tutorial on how this is done. There are very few developer uses this at this time. So, if you want to be called as a "Developer", then you should learn this, including the phpUnit. Thriving in this type of job market is pretty difficult, unless you truly know the ins and outs of PHP..

Edited by veedeoo: info added

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.