Re: String passed to object constructor turning into aninstance of that object?

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

 



On Thu, Dec 16, 2010 at 3:21 PM, Kris Deugau <kdeugau@xxxxxxxxx> wrote:

> Nathan Nobbe wrote:
>
>> Why not test for the type of $name at each point of interest in the
>> SelectBoxOption
>> constructor?  If you're passing a string value to the constructor it
>> almost
>> has to be getting changed by the Tag constructor, right ?
>>
>>  class SelectBoxOption extends Tag {
>>   function SelectBoxOption($name, $value, $selected=false) {
>>
>> var_dump(is_string($name));
>>
>>     parent::Tag("option", $name);
>>
>> var_dump(is_string($name));
>>
>
> Ah, that gives...  well, it slightly alters the confusion.
>
> Using var_dump(is_string($name)) gives...  two results?
>
> bool(true)
> bool(false)
>

so you put one check before the call to parent::Tag() & one directly after
right?  That means *somehow* $name is getting set to an instance of
SelectBoxOption in the parent constructor which makes little to no sense..
especially after looking at implementation from your later post.  Main
things are $name is local in the child constructor and there is no pass by
reference on the $name parameter in the parent constructor definition.

if this code runs w/o error on your 5.2 box, then there's something spurious
going on in that old library;

<?php
class Tag
{
    function Tag($sTag='', $sValue='')
    {
        $this->_sTag = $sTag;
        $this->_sValue = $sValue;
    }
}

class Child extends Tag
{
    function Child($name)
    {
        var_dump($name);
        parent::Tag('option', $name);
        var_dump($name);
    }
}

$oChild = new Child('content');
?>

expected output:

string(7) "content"
string(7) "content"

I'd still recommend moving to the php5 notation throughout the library,
especially if doing that fixes the problem w/ SelectBoxOption.  This
shouldn't break any client code, since clients should all be calling new
Class() and not be explicitly invoking the php4 style constructors.  The
php4 style constructors should only be getting called explicitly from within
the library itself.

-nathan

[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