First of all, for the last two years I have been developing for Java, so my OOP-views are "contaminated" by that...

I have one very easy problem that's bugging me.

I like to split my classes into seperate files, one for each class.

The problem arises when one class extends or uses another.
The convienient way of making sure that everything gets included would be to require_once('parentClass') in the extending class, but then I can't call the parent::contructor().

See example below...


//-----------------------------------
// file A.php
//-----------------------------------

class A {
  function A() { echo "A"; }
}

//-----------------------------------
// file B.php
//-----------------------------------


/* I would like to be able to do this */
require_once 'A.php';

class B extends A {
   function B() {
      parent::A(); // Call the parents constructor
      echo "B";
   }
}

-----------------------------------

// file test.php

/* I HAVE to do this in order to get the "parent::"-thing working! */
require_once 'A.php';
require_once 'B.php';

/* I would like to only include B.php like this */
require_once 'B.php';

$anInstance = new B();

I am currently developing for a server which only supports PHP4.

Any thoughts?

    / I would like to only include B.php like this /
    require_once 'B.php';

    $anInstance = new B();

    That works for me, as expected (v4.3.8).

      Write a Reply...