Skip to content

tomnomnom/flatclass

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flatclass

Given an object or class name, the Flatterer returns a 'flattened' version of a class; i.e. with non-overridden methods and properties from parent classes copied into it.

For example, the following code:

<?php
require __DIR__.'/vendor/autoload.php';

interface Nameable {
    public function getName();
}

class Animal implements Nameable {
    protected $name;
    public function __construct($name){
        $this->name = $name;
    }
    public function getName(){
        return $this->name;
    }
}

class Dog extends Animal {
    public function bark(){
        return "Woof!";
    }
}

$f = new \Flatclass\Flatterer('Dog');
echo $f->flatten('FlatDog');

Would produce a FlatDog class that looked like this:

<?php
class FlatDog extends \Animal implements \Nameable {

    protected $name;

    // From class Dog
    public function bark(){
        return "Woof!";
    }

    // From class Animal
    public function __construct($name){
        $this->name = $name;
    }

    // From class Animal
    public function getName(){
        return $this->name;
    }

}

Why?

This is meant as a debugging tool. With very deep inheritance hierarchies it can be difficult to mentally follow code execution paths that jump back and forth between several files.

While the generated class is not guaranteed to function in the same way as the original, some efforts are made to make it more likely; e.g. the resulting class has the same parent as the original to allow parent::method() calls to work.

About

Flatten deep inheritance trees in PHP to aid debugging

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published