Hi Sebastian and PHP list,

That's a very interesting idea and I'd use it too. Yesterday night I
was developing a set of classes that it could apply this idea too.


Altho I found this idea really interesting, I have a suggestion for
it. Instead of use a function to handle new overloads, I suggest a
magic method, something like __new.

The idea behind this:
<?php

class Foo {
    function __construct($type = 'Foo') { echo 'Created ', $type; }
    function __new( $type ) {
        if ($type == 'Bar') {
            return new Bar();
        }
    }
}

class Bar {
    function __construct() { echo 'Bar here!'; }
}

$bar = new Foo('Bar'); // Creates Bar instance

?>


Dunno the impact of this in the source, but my humble idea is that it
independs of inheritance. Of course this would be more acceptable for
super and extended classes, and here is a true usage of it:


class Validator {
    private $_country;

    function __construct() {}

    function __new($country) {
        switch ($country) {
        case 'Brazil': return new Validator_Brazil();
        default: return new Validator_US();
        }
    }
}

interface IValidator_Country  {
    function validate($type, $value);
}

class Validator_Brazil implements IValidator_Country {
    function __construct() { ... }
    function validate($type, $value) { ... }
}

class Validator_US implements IValidator_Country {
    function __construct() { ... }
    function validate($type, $value) { ... }
}

$o = new Validator('Brazil'); // Creates Validator_Brazil

echo ($o->validate('zip', '13561000')) ? 'Zip code is valid' : 'Zip is
not valid';



The basic idea is to check for __new existance and call it instead of
__construct. If there's no return or null after __new call, then call
the __contruct as currently it does.


So, what do you think??? =)



Regards,


On 10/2/07, Sebastian Bergmann <[EMAIL PROTECTED]> wrote:
>  From [1]:
>
>    Objective-C permits a class to wholly replace another class within a
>    program. The replacing class is said to "pose as" the target class.
>    All messages sent to the target class are then instead received by
>    the posing class.
>
>    There are several restrictions on which classes can pose:
>
>      * A class may only pose as one of its direct or indirect
>        superclasses.
>
>      [The other restrictions do not apply to PHP]
>
>  Earlier this year, Johannes implemented class posing for PHP as follows:
>
>    <?php
>    class Foo {}
>    class Bar extends Foo {}
>
>    function new_overload($className)
>    {
>        if ($className == 'Foo') {
>            return new Bar;
>        }
>
>        // ...
>    }
>
>    $o = new Foo;
>    // $o is an object of Foo.
>
>    register_new_overload('new_overload');
>
>    $o = new Foo;
>    // $o is an object of Bar.
>    ?>
>
>  We (Johannes, Marcus, Sara, and myself) then discussed where to put this
>  functionality. Outside of core, there were two places that both make
>  sense: pecl/operator and pecl/runkit.
>
>  However, to make this a viable mechanism that can be used in tools such
>  as PHPUnit (for which I could really use this functionality), we agreed
>  that it actually belongs into the core.
>
>  Opinions? Needless to say that I would love to see this in PHP 5.3 ;-)
>
>  --
>  [1] http://en.wikipedia.org/wiki/Objective_C#Posing
>
> --
> Sebastian Bergmann                          http://sebastian-bergmann.de/
> GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Guilherme Blanco - Web Developer
CBC - Certified Bindows Consultant
Cell Phone: +55 (16) 9166-6902
MSN: [EMAIL PROTECTED]
URL: http://blog.bisna.com
São Carlos - SP/Brazil

Reply via email to