Skip to content

Instantly share code, notes, and snippets.

Last active May 16, 2019
What would you like to do?
PHP - Rewrite dependencies classes

Class Alias


namespace Shop\Models;

use Shop\Models\Address as AddressClass;

/** @var Address parent */
class_alias(defined('AddressClass') ? AddressClass : Address::class, 'ThingPink\Shop\Models\AddressClass');

 * Class BillingAddress
 * @package Shop\Models
class BillingAddress extends AddressClass
  • Can simply extend the class that we need to overwrite some function and point the alias to it (do not need rewrite the entire class)
  • Every file with this strategy lose IDE type hints from parent and eventually every type hints for classes with this strategy
  • Need to add this alias code to EVERY class in order to make em "replaceable"

Class overwrite


  "autoload": {
    "psr-4": {
      "Shop\\Models\\": "./extended"

Where inside the ./extended folder exists the class(es) that you want to reimplement.

  • Do not need to change a thing in vendor classes in order to adapt extensibility
  • IDE automatically just read the "overwritten" version, meaning that we get out of the box all the IDE hints
  • Need to rewrite entire classes (instead just extend em and overwrite the desired changes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment