0

I have a following php class.

<?php

class t extends c{
  function __construct() {    
    parent::__construct();   
  }
}
class c extends d{
   function __construct() {    
    parent::__construct();   
   }
}
class d {
  function __construct() {          
    echo "worked";
  }
}

new t();
?>

The above class is working fine in my local machine where php version is (PHP Version 5.5.9-1ubuntu4.2)

but its not working in cloud server where php version is (PHP Version 5.4.26 and linux hosting) I have another server where php version is (PHP Version 5.3.28 amazone cloud server) Here the above code is also not working.

Any idea why its not working in above two php version(5.4.26 and 5.3.28)?

1
  • What do you mean by "not working"? Do you get an error? Commented Jul 8, 2014 at 21:16

1 Answer 1

1

You see here in this example that you get this error:

Fatal error: Class 'c' not found in /tmp/execpad-0fdb5d0d9043/source-0fdb5d0d9043 on line 3

If you switch the order of your class declaration around so that it is defined in a logical (procedural I suppose), you get what you expect (PHP 5.4.6):

class d { /* etc */ }

class c extends d { /* etc */ }

class t extends c { /* etc */ }

new t(); // worked

See the PHP manual on object inheritance:

NOTE: Unless autoloading is used, then classes must be defined before they are used. If a class extends another, then the parent class must be declared before the child class structure. This rule applies to classes that inherit other classes and interfaces.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.