Jump to content

[SOLVED] Class extending


haaglin

Recommended Posts

Hi.

 

Is it possible to have 1 class to extent 2 separate classes? I have 2 classes that are different, but they share a lot of the same functions..

 

In this example, i want class one and two to have the functions from class three, but i do not want class one to have the functions of class two..

class one {
    function one() {
    }
}
class two {
    function two() {
    }
}

class tree {
    function tree() {
    }
}

Link to comment
Share on other sites

Any number of classes can inherit from a single parent class. If class A is your parent, then class B,C,D etc can all be children of that class, without inheriting from each other.

 

However, php does not support multiple inheritance, which is where a class extends multiple classes - that is, it has more than 1 parent. For example, class C cannot inherit from class A and B.

 

You question wasn't entirely clear, however i think you wanted something like:

<?php

class three{
function three(){

}
}
class one extends three{
function one(){

}		
}
class two extends three{
function two(){

}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.