Web Analytics Made Easy -
StatCounter PHP inheritance - CodingForum


No announcement yet.

PHP inheritance

  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP inheritance

    I have been trying to understand better inheritance in PHP and had a question. When write someting like this:

    PHP Code:
     class SnPayPalApi extends SnPayPalApiInterface 
    How does the code know which file to look in for the SnPayPalApiInterface class. They are two different files so without including anything or setting a class root how does the code know where to look for the parent class?

  • #2
    The parent class has to go above the child class. If they are in two seperate files. Then i guess look for the part of code that creates a new instance of the child class with new classname . That file will probably have the individual files included using require, include, or require_once. But basically to answer your question

    How does the code know which file to look in for the SnPayPalApiInterface class
    It doesn't you have to define the class before it gets there. As it says here

    Note: Classes must be defined before they are used! If you want the class Named_Cart to extend the class Cart, you will have to define the class Cart first. If you want to create another class called Yellow_named_cart based on the class Named_Cart you have to define Named_Cart first. To make it short: the order in which the classes are defined is important.
    Last edited by timgolding; Apr 14, 2009, 06:25 PM.
    You can not say you know how to do something, until you can teach it to someone else.