3

Is it possible to either

extend an extension of a class e.g.:

class a {
}
class b extends a {
}
class c extends b {
}

EDIT:

[or some other way that class c can use functions only defined in b but not in a.]

Well I just notice that my question was kinda messed up. c should be able to use all functions, so of course those of a aswell.

I'll just accept the answer solving my non-existing problem, in case someone searches for this.

3
  • So what was your actual problem? Commented Sep 4, 2012 at 14:14
  • Whether it is possible to extend "extends" Commented Sep 4, 2012 at 14:16
  • Ah, okay. Yeah, it is :) Commented Sep 4, 2012 at 14:16

3 Answers 3

4

Since PHP 5.4 you can use traits which may be of help to you. You can define two traits, one with the functions that class a has and one containing functions that class b has, but not class a.

So:

trait TraitA {
    function a1() { }
    function a2() { }
}

trait TraitB {
    function b1() { }
    function b2() { }
}

Then you can define a class a that uses only TraitA, a class b that uses both traits, and a class c that uses only TraitB:

class a {
    use TraitA
    // contains a1 and a2
}

class b {
    use TraitA, TraitB
    // contains a1, a2, b1 and b2
}

class a {
    use TraitB
    // contains b1 and b2
}

More on traits: http://php.net/manual/en/language.oop5.traits.php Keep in mind that you have to pay attention to your function names between different traits. You can't have the same functionnames in both traits or you'll get collisions.

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

4 Comments

Damn you just got there before me!
sadly my host doesn't have php 5.4
Traits look horrible. Nothing wrong with proper organisation of classes. Looks like botched multiple inheritance. Gonna create some confusing code.
I myself am not sure whether I will ever use it. I am getting used to the way they look, but it took me quite a while too... So far I haven't been in a situation where they are useful.
3

This isn't possible. By definition class B is an "extension" of class A. This means that class if class a has the method method_in_class_a() then class B will have method_in_class_a() as a BASE before you even configure the class.

All you need to do is to do it the other way around:

class b { ... }

class a extends b { /* has methods from b and a */ }

class c extends b { /* can only use methods from b */ }

Comments

1

I'm not sure why you need to do this but...

Unless methods and properties are defined as private methods, all extending classes will have access to them and can be extended, unless the parent method is declared as final.

However, since PHP5.4, you can use Traits.

<?php

trait BOnly 
{
    function getReturnType() { /* 1 */ }
    function getReturnDescription() { /* 2 */ }
}

class A 
{

}

class B extends A
{
    use BOnly;  
}

class C extends B
{

}

2 Comments

The way you do it, class C still contians the stuff from class A.
Yeh I just quickly put an example together. I've never used them myself, and probably won't, at least not for a while.

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.