How to Extend a Class Using More Than 1 Class in PHP

Can I extend a class using more than 1 class in PHP?

If you really want to fake multiple inheritance in PHP 5.3, you can use the magic function __call().

This is ugly though it works from class A user's point of view :

class B {
public function method_from_b($s) {
echo $s;
}
}

class C {
public function method_from_c($s) {
echo $s;
}
}

class A extends B
{
private $c;

public function __construct()
{
$this->c = new C;
}

// fake "extends C" using magic function
public function __call($method, $args)
{
$this->c->$method($args[0]);
}
}


$a = new A;
$a->method_from_b("abc");
$a->method_from_c("def");

Prints "abcdef"

Can you extend two classes in one class?

You can extend the child class to inherit both its parent and the child's functions, which I think is what you are trying to do.

class Parent
{
protected function _doStuff();
}

class Child extends Parent
{
protected function _doChildStuff();
}

class Your_Class extends Child
{
// Access to all of Parent and all of Child's members
}

// Your_Class has access to both _doStuff() and _doChildStuff() by inheritance

PHP - Extend multiple classes

Php doesnot allow multiple inhritance. There are two ways to do this -

First

class a { }

class b extends a { }

class c extends b { }

Interfaces -

interface sb { }

class c extends a implements sb { }

Multiple inheritance, 'need' to extend two classes

PHP doesn't support multiple inheritence. You can however rewrite your logic using traits to (sort of) simulate it.

class Field {
protected $name;
protected $value;
}

trait Dropdown {
protected $options = [];
// with functions like setOptions(), addOption(), removeOption() etc.
}

interface IntegrationPositions {
const LAYOUT_POSITION_LEFT = 'left';
const LAYOUT_POSITION_RIGHT = 'right';
}

trait Integration {
protected $layoutPosition = IntegrationPositions::LAYOUT_POSITION_LEFT;
}

class DropdownField extends Field {
use Dropdown;
}

class IntegrationField extends Field {
use Integration;
}

class DropdownIntegrationField extends Field {
use Integration,Dropdown;
}

Update: As @Adambean noted traits cannot have constants. I've therefore updated the example using an enum.

This feels strange to have to declare an enum that's meant to be internal to a trait but PHP does not seem to allow any other mechanism to achieve this as far as I know, I am open to any other ideas.

How to extend multiple utility classes

No

Your idea leads to multiple inheritance and it is not available in PHP for good.

I extremely recommended you read this question to see why you shouldn't try this.


However, you still can chain the classes. If done in a proper way you can get the functionality of all classes.

Class Observer {}
Class Logger extends Observer {}
Class OtherUtilClasses extends Logger {}

//Now
class MyClass extends OtherUtilClasses {}

PHP Multiple Classes Extending One

If you really need the methods two, three and four in your Main class without implementing them you'd have to make it abstract and force the childs to implement all of them (this is absolutely acceptable and a good way of doing things like this).

<?php

abstract class Main {

function fn1() {
$this->fn2();
$this->fn3();
}

abstract function fn2();

abstract function fn3();

}

class Child extends Main {

function fn2() {
return "foo";
}

function fn3() {
return "bar";
}

}

?>

Another alternative is to check if the methods exist (not good / hack / see comment below).

<?php

class Main {

function fn1() {
if (method_exists($this, "fn2")) {
$this->fn2();
}
if (method_exists($this, "fn3")) {
$this->fn3();
}
}

function fnDontRepeatYourself() {
foreach (range(2,3) as $fn) {
if (method_exists($this, $fn)) {
$this->{$fn}();
}
}
}

}

?>

The problem with the second approach is that the childs don't actually know that they have to implement the methods, that's problematic.

The inheritance should go from general to specific and you should not echo anything in your classes, rather return. There is a lot of things I could tell you now about what to do and what not, but I think it would be best for your to learn more about object oriented theory first before continuing. :)

How to extend multiple classes in CodeIgniter?

As mentioned, multiple inheritance is not allowed in php, but if you need to access functions in the Appointments class, why not just create an instance of it in the class and call the function on that instance?

$ins = new Appointments();
$result = $ins->someFunction();

How to extends multiple classes, in php?

You're doing all wrong.

symfony 1 is completely different than Symfony 2 (check this article: How Symfony2 differs from symfony1). You follow a tutorial from sf1 to create form in sf2.

You should instead try to find resources for sf2, like :

  • Official Form documentation
  • Lots of recipes in the official Form Cookbook

And since you're french, I recommend you to read these tutorials:

  • Les formulaires dans Symfony2
  • Créer des formulaires avec Symfony2

Then, when you will have read these tutorials, you can come back here to ask a new question about a problem you got when implementing a form.



Related Topics



Leave a reply



Submit