renosis
renosis

Reputation: 2722

Including class files PHP

I have been trying to learn about classes in PHP and part of my learning process has been looking at various libraries and stuff out there. Of course I am left with many questions, here is one of them.

When looking at some libraries, the main file that you call to invoke the library, they call each of the libraries files by invoking them directly by name, for example this is how the PHP chart library does it:

<?php
require_once 'model/ChartConfig.php';
require_once 'model/Point.php';
require_once 'model/DataSet.php';
require_once 'model/XYDataSet.php';
require_once 'model/XYSeriesDataSet.php';

require_once 'view/primitive/Padding.php';
require_once 'view/primitive/Rectangle.php';
require_once 'view/primitive/Primitive.php';
require_once 'view/text/Text.php';
require_once 'view/color/Color.php';
require_once 'view/color/ColorSet.php';
require_once 'view/color/Palette.php';
require_once 'view/axis/Bound.php';
require_once 'view/axis/Axis.php';
require_once 'view/plot/Plot.php';
require_once 'view/caption/Caption.php';
require_once 'view/chart/Chart.php';
require_once 'view/chart/BarChart.php';
require_once 'view/chart/VerticalBarChart.php';
require_once 'view/chart/HorizontalBarChart.php';
require_once 'view/chart/LineChart.php';
require_once 'view/chart/PieChart.php';
?>

However, I look at another library, and they just automate the process by calling each file in the directory using the PHP autoload method like this(From the php manual):

function __autoload($name) {
    echo "Want to load $name.\n";
    throw new Exception("Unable to load $name.");
}

I was just wondering if there is any advantage or disadvantage to doing it the autoload way?

Upvotes: 2

Views: 3541

Answers (5)

Gunnar Wrobel
Gunnar Wrobel

Reputation: 181

I think I would agree most with Cygal but would like to amend the response taking PSR-0 into account. I don't think it makes much sense using autoloading if you don't adhere to that standard.

Pro Autoloading

  • No need to maintain the include list.
  • You only include what you really need.
  • Code remains readable.
  • You follow a standard adopted by all major PHP frameworks so you can expect that people are familiar with this approach.

Pro old style

  • The dependencies are easily visible in the header of the code files.
  • There is no mapping between class names and file paths necessery. If you use autoloading a lot it might be necessary to reduce the time wasted for repeatedly calculating this mapping by using a cache.

To summarize: I don't see any reason to avoid autoloading as long as you follow PSR-0!

Upvotes: 1

Quentin Pradet
Quentin Pradet

Reputation: 4771

I don't believe __autoload only has advantages. But there are some indeed.

Pros for __autoload

  • You don't need to maintain that long list of includes, which can be a pain to keep up tidy (which means there's no forgetting an include)
  • You only include what you really need: some classes might only be needed in some special scenario, you can avoid it

Pros for the normal way

  • It's very easy to understand for anybody
  • Looking at the includes will tell you what kind of things the file is doing
  • You control the list of classes you're importing (if you don't want a class because it's huge or doesn't work, it will never get included)
  • There's no complexity in the __autoload function. If your classes are in a complex hierarchy, you just need to type the name, and don't need to write complex code to find the correct file.
  • Easier to debug: the scope of the code that could be worrying is limited to the includes you did.

It's really a matter of coding style.

Upvotes: 3

PeeHaa
PeeHaa

Reputation: 72729

The advantage if autoloading files is you don't need to worry about loadng the files yourself. And the files only gets loaded when they are needed.

An even better autoload function would be the newer: http://php.net/manual/en/function.spl-autoload-register.php

Upvotes: 0

Chris Fletcher
Chris Fletcher

Reputation: 2387

What Mark said. Also, you can keep your code DRY, if that's important to you.

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212522

Using an autoloader is always advantageous.

  • You don't need massive collections of includes at the top of every file
  • You don't have to worry about forgetting an include
  • Autoloader only includes files that are necessary, and as they are needed

Potential drawbacks

  • Clash between your aotoloader and autoloaders used by libraries you want

Potential drawback can be avoided by registering your autoloader with spl

Upvotes: 0

Related Questions