2017 © Pedro Peláez
 

library constructor-overloading

Constructor overloading for PHP 5.3

image

constructor-overloading/constructor-overloading

Constructor overloading for PHP 5.3

  • Monday, October 1, 2012
  • by dorireuv
  • Repository
  • 1 Watchers
  • 1 Stars
  • 19 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Constructor Overloading

A library which allows you to have constructor overloading in PHP >=5.3 projects. This is simply done using: type hint, default value and PHP doc., (*1)

Usage

In order to use the library it's best recommended to follow some rules. The library is looking for any private / protected methods width start with _construct in your class. Among these methods the best candidate is picked up. In order to help the library to find the best candidate you shall follow these rules:, (*2)

  1. If your argument is an object, use type hint (example: function(A $a)).
  2. Otherwise, the argument is a primitive and in this case if it has a default value, then you can use it to hint its type (example: function ($int = 0)).
  3. Otherwise you can use PHP doc (example: @param int $int)

** In case of a primitive has default value and PHP doc the default value will be preferred over the PHP doc., (*3)

Examples

First, in order to use the library, one must register the autoloader:, (*4)

<?php

use ConstructorOverloading\ConstructorOverloading;
$pathToConstructorOverloading = __DIR__ . '/ConstructorOverloading';
require_once $pathToConstructorOverloading . '/ConstructorOverloading.php';
$constructorOverloading = new ConstructorOverloading();
$constructorOverloading->registerAutoloader();

Then, let's say you have a class called Rectangle which can be constructed by either width and height or by size (square):, (*5)

<?php

use ConstructorOverloading\Dispatcher;

class Rectangle
{
    private $width, $height;

    public function __construct()
    {
        $dispatcher = new Dispatcher();
        $dispatcher->dispatch($this, func_get_args());
    }

    /**
     * @param int $width
     * @param int $height
     */
    protected function _constructWidthAndHeight($width, $height)
    {
        $this->width = $width;
        $this->height = $height;
    }

    /**
     * @param int $size
     */
    protected function _constructSize($size)
    {
        $this->width = $this->height = $size;
    }
}

Now, if you use:, (*6)

$rectangle = new Rectangle(10, 20);

The _constructWidthAndHeight will be invoked and result in width = 10, height = 20, (*7)

And, if you use:, (*8)

$rectangle = new Rectangle(30);

The _constructSize will be invoked and result in width = height = 30, (*9)

The Versions

01/10 2012

dev-master

9999999-dev http://github.com/dorireuv/constructor-overloading

Constructor overloading for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Dori Reuveni

overloading constructor

01/10 2012

2.0

2.0.0.0 http://github.com/dorireuv/constructor-overloading

Constructor overloading for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Dori Reuveni

overloading constructor

30/09 2012

1.0

1.0.0.0 http://github.com/dorireuv/constructor-overloading

Constructor overloading for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Dori Reuveni

overloading constructor