14

I am used to working in languages such as C#/Java/Python where each class would have its own file, and for a class to see other classes, you would import the package containing those classes. How does this work in php? The documentation shows you how to create classes, but I don't understand how it all fits together in a php context. I know of the include statement, which just sticks the files together basically.

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
BobTurbo
  • 891
  • 3
  • 11
  • 14
  • 3
    possible duplicate of [Import package or autoloading for PHP?](http://stackoverflow.com/questions/616231/import-package-or-autoloading-for-php) – Gordon Jan 21 '11 at 11:05
  • 2
    *(tipp)* search for autoloading and code conventions in PHP – Gordon Jan 21 '11 at 11:06
  • Personally, I find it very sensible to have each class in its own file... but use an autoloader rather than java's import – Mark Baker Jan 21 '11 at 11:07
  • @middaparka: I was the subject of a completely off-topic (although short-lived) discussion just the other day. – BoltClock Jan 21 '11 at 11:18
  • @middaparka: The comment that started it was removed by flagging, but contextual remnants can be found in meagar's reply [here](http://stackoverflow.com/questions/4717369/conditional-loop). – BoltClock Jan 21 '11 at 11:49
  • Check out psr-4 autoloading using Composer - https://getcomposer.org/ . It will greatly help with organization, especially in the realm of namespacing. – Douglas.Sesar Mar 11 '15 at 11:34

5 Answers5

16

You can use __autoload

function __autoload($class_name) {
    include 'classes/'.$class_name . '.php';
}

So place every single class in its own file in the classes folder. When you want to use that class it will include it. More info: http://php.net/manual/en/language.oop5.autoload.php

Update: When I answered this it was fully valid. Now it still works, but keep in mind PHP.net since then says this:

spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future.

Ákos Nikházy
  • 1,276
  • 17
  • 33
  • 2
    Also take a look at: http://www.php.net/manual/en/function.spl-autoload-register.php – Mchl Jan 21 '11 at 11:22
5

The easiest way:

  • define your classes in "classes" directory
  • init application like shown below
  • name class filenames as their lowercase class name with .php suffix (MyClass => classes/myclass.php)

Init code:

set_include_path ( "./classes" );
spl_autoload_register ();

//class is automatically loaded from ./classes/myclass.php
$object_instance = new MyClass ();
Kamil Tomšík
  • 2,415
  • 2
  • 28
  • 32
3

As of PHP 5.3.0 , it is recommended that you use the spl_autoload_register() function because __autoload() is said to be deprecated some time in the future.

An easy way to use this function:

1) Place each class file into the 'classes' folder

2) Run an anonymous function inside spl_autoload_register() which specifies your class folder:

spl_autoload_register(function ($class) {
    include 'classes/' . $class . '.php';
});

Now when you try to use a class that is not defined in your code yet, it will check that class folder one last time before giving you an error.

Douglas.Sesar
  • 4,214
  • 3
  • 29
  • 36
  • This is cool but this is best used when you want your own autoloader with its own name as described in Example #1 here: http://php.net/manual/en/function.spl-autoload-register.php In this case __autoload is enough. – Ákos Nikházy Mar 11 '15 at 11:39
1

Imagine you have been making your object in PHP in a file called myObject.php

<?php

  class myObject
  {
    public function __construct()
    {
      echo "Hello, World";
    }
  }

?>

And in another file, you would like to use the object (let's call this myfile.php). You would have to include your object - like this:

<?php

  include("myObject.php");

  $intance = new myObject();

?>

Quite simple.

Repox
  • 15,015
  • 8
  • 54
  • 79
0

In PHP you can do it in various ways, compiler does not limit you.

You can have 1 class in 1 file, 5 classes in 1 file, 1 class across several files using includes...

But usually it's still 1 class in 1 files, and if you have many tiny ones - you can have them in 1 folder too.

When doing 1 class in 1 file with the same name you can set up class autoload so that you don't need to write your includes.

BarsMonster
  • 6,483
  • 2
  • 34
  • 47
  • You can NOT have a class split over multiple files. See here for example: http://stackoverflow.com/questions/1957732/can-i-include-code-into-a-php-class/1957830#1957830 – yokmp Aug 03 '15 at 18:20