Re: Class & Type Casting

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jun 10, 2008 at 5:42 AM, Richard Heyes <richardh@xxxxxxxxxxx> wrote:
>> //that's working fine
>> $test= new string_extended("this is my anonymous string");
>> echo $test;
>>
>>
>> /that's not working, but should'nt it be the case ?
>> $test = (string_extended) "This is my anonymous string";
>> echo $test;
>>
>> I'll find this very usefull :x, it's just a syntax ehancement nope ?
>
> Can't give you a definite answer but presumably it's not calling the
> constructor when you type cast it. Add a line to your constructor that will
> show if this is the case. Eg:
>
> function __construct($str){
>    $this->contents=$str;
>    echo "In string_extended constructor...<br />\n";
> }

True -- and besides, that's not exactly what casting is for anyway. In
your example, there is no way for the computer to know that "This is
my anonymous string." should be passed to the constructor or assigned
to an internal property of the class.

Type casting (at least as far as I've ever seen it) can only be done
between members of the same hierarchy -- usually in cases where you
have an instance of a child class stored in a variable allocated as
one of its ancestors. The type casting is done to be able to use
methods of a more specialized child class that are not defined for the
parent.

<?php


class MyBaseString {

    protected $_contents = '';

    public function __construct($str) {
        $this->_contents = $str;
    }

    public function __toString() {
        return $this->_contents;
    }
}

class MySpecialString extends MyBaseString {

    public function toUpperCase() {
        return strtoupper($this->_contents);
    }

}


function showString(MyBaseString $someString) {
    // In Java, you would have to do this to be able to call the toUpperCase
    // method since it is not part of the MyBaseString data type.
    // echo (MySpecialString) $someString->toUpperCase();

    // In PHP you can call it directly, though it is admittedly problematic
    // since it violates the 'contract' of the method signature!
    echo $someString->toUpperCase();
}


$str = new MySpecialString('This is my anonymous string.');

showString($str);

echo "\n";


$str2 = new MyBaseString('Hello World!');

showString($str2);

?>

You can cast your object to a string, but only because there is a
magic method defined in the language to allow that and not because
your object is based on the PHP data type. Variables are not strongly
typed in PHP, and PHP strings are handled like primitives rather than
instances of a class that extends the base Object class like they do
in Java. (PHP primitives can be converted to an instance of stdClass,
but it's not the same. AFAIK, stdClass does not have any methods of
its own, either.)

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux