php - inheritance and external files. How do I go about it? -


i have 3 files.

  • vehicleclass.php
  • motorbikeclass.php (extends vehicleclass)
  • index.php

my question is... how connect 3. on index page, have have

include 'classes/vehicleclass.php'; 

do need have include motorbike class or extend (inheritence) cover this?

you can let php autoload files, registering own autoload function. if have, in example, class files in directory docroot . 'classes' (docroot being document root), can use function (example):

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

now if try create object of class 'foo', try include file docroot . '/classes/foo.class.php' before creating object. might want extend function bit, eg. lowercase file names (include docroot . 'classes/'. strtolower($class) .'.class.php';). way can place class foo in foo.class.php.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -