1

I have 3 files.

  • vehicleClass.php
  • motorbikeClass.php (extends vehicleClass)
  • index.php

My question is... How do I connect all 3. On my index page, do I have to have

include 'classes/vehicleClass.php';

Do I need to have an include for the motorbike class or will the extend (inheritence) cover this?

kero
  • 10,647
  • 5
  • 41
  • 51
WayneP
  • 139
  • 1
  • 1
  • 6
  • Why don't you try it out? If you manually want to include all necessary files, I'd recommend `require_once` so you don't get a conflict when including the same class twice - even better: use autoloading! – kero Jan 31 '14 at 16:57

3 Answers3

1

Short answer: a class that is extending (or otherwise using) another class must already have defined the parent class before the definition of the child class. Your assumption is correct, your VehicleClass must be included (or better, require'd) prior to your definition of MotorBike class.

However, most frameworks don't go about and include every depedency before all class definitions. This would become unwieldy on any system that has any amount of complexity to it. Instead, the developers of PHP have provided methods for autoloading classes. Using spl_autoload_register will allow you to write a function that will attempt to load in the source file for a given class whenever it is referenced but a definition for it has not yet been found.

Furthermore, once you get a system together that becomes complex, you don't want to store all of your files in a single place. Most frameworks leverage the filesystem and namespaces to help better organize all of their classes. Because of this, the PSR-0 standard was developed in order to help facilitate autoloading between frameworks. Take a look at this question for examples of PSR-0 compliant autoloaders.

Example of PSR-0 compliant class:

<?php namespace Vendor\Package;

class ClassName { }

This file would live in the filesystem at /Vendor/Package/ClassName.php

Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
1

You can let php autoload your files, by registering your own autoload function. If you have, in example, all your class files in the directory DOCROOT . 'classes' (DOCROOT being your document root), you can use a function like this (example):

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

spl_autoload_register('class_loader');

Now if you try to create an object of class 'foo', it will try to include the file DOCROOT . '/classes/foo.class.php' before creating the object. You might want to extend the function a bit, eg. to lowercase file names (include DOCROOT . 'classes/'. strtolower($class) .'.class.php';). This way you can place class Foo in foo.class.php.

giorgio
  • 10,111
  • 2
  • 28
  • 41
0

What you have to do is include 2 files in the index.php.

For example, your index.php page could be something like this.

<?php
require 'classes/vehicleClass.php'; 
require 'classes/motorbikeClass.php';

// Assuming your class name is MotorBike 
$motorBike = new MotorBike();


// And just call the method you want, for example If you have a method called bikeName
echo $motorBike->bikeName();

?>

I hope you get an idea now.

P/S: I prefer require over include. :) Include() should work fine too.

haris
  • 3,775
  • 1
  • 25
  • 28