Re: [PHP] String passed to object constructor turning into aninstance of that object?
David Harkness wrote: I've never used the old-style constructors, but perhaps the semantics of parent:: changed and you need to instead use $this- as in $this-Tag(option, $name); That's a total guess. I don't have 5.2 handy to try it out, but both work in 5.3 using a simple example. Can you post the constructor of one of the Tag subclasses that work? Maybe we can find a common denominator. The most similar is Column, but all of them do very similar things - it's just the one class that seems to take a string and mutate it into what looks like an array with a string at [0] and something closely resembling what the whole object instance *should* be at [1]. class Table extends Tag { function Table() { parent::Tag('table'); $this-addAttribute('cellspacing', 0); $this-addAttribute('cellpadding', 0); $this-addAttribute('border', 0); $this-columns = array(); $this-rows = array(); } class Row extends Tag { function Row($table='') { parent::Tag('tr'); $this-table = ''; } class Column extends Tag { function Column($data) { parent::Tag('td', $data); $this-tagContent = $data; } class FormObject extends Tag { function FormObject($name='') { parent::Tag(); $this-addAttribute(name, $name); $this-name = $name; } -kgd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String passed to object constructor turning into aninstance of that object?
Nathan Nobbe wrote: 2. try modifying Tag SelectBoxOption to have __construct() instead of Tag() SelectBoxOption(), then call parent::__construct() from inside of SelectBoxOption::__construct(); see if that clears up your problem under 5.2 (read: this will only be a partial solution as it only addresses one child of Tag). Mmm. I hoped this would help, but all it seems to have done was cascade errors across the rest of Tag's object children. :( to be expected, but did it fix the problem w/ SelectBoxOption? I'm not sure, but I don't think so; the original symptom was Object of class SelectBoxOption could not be converted to string - the original code didn't include a toString() method in SelectBoxOption, and since the code works on an older PHP, it must be using the parent object's toString(). (Which some children of Tag do explicitly, but SelectBoxOption doesn't for whatever reason.) In trying to add the toString() method, I found that the calls used by other tags to retrieve the HTML tag name, value, etc weren't working. So I looked up at the constructor to see if the pieces passed in were getting passed and stored correctly - and quite obviously they're not ($name mutates into what looks like an array with a string at [0] and something closely resembling what the whole object instance *should* be at [1], and $value just seems to disappear). Putting aside actually fixing the constructor correctly, after a bit of poking I found that $this-tagContent-. works to retrieve the data and actually output the option tag correctly. var_dump tells me that the real data is actually in there... it's just not instantiated correctly. $name apparently arrives at the constructor for SelectOptionBox like this: string(8) Abegweit object(SelectBoxOption)#65 (5) { [attributes]= array(1) { [0]= object(TagAttribute)#66 (3) { [name]= string(5) value [value]= string(1) 4 [hasValue]= bool(true) } } [tagContent]= string(8) Abegweit [tag]= string(6) option [showEndTag]= bool(false) [children]= array(0) { } } I'll try converting all of the constructors to your recommendation as above, but given that the problem is only happening with this one class, I'm not sure that will do much. hopefully that clears it up .. Well, I ran out of Call to undefined ParentClass::parentclass in path/to/file/for/subclass.php errors (at least on the page I'm testing with) but $name is still going in on the calling side as a string, and coming out as a funky array. and hopefully you're using version control :D Bah! Real man never make mistaaake! ... ooops. g (I've got the live site, on the old server, as reference, plus the regular backups of that machine.) -kgd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String passed to object constructor turning into aninstance of that object?
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) And dumping $name itself gives: string(8) Abegweit object(SelectBoxOption)#65 (5) { [attributes]= array(1) { [0]= object(TagAttribute)#66 (3) { [name]= string(5) value [value]= string(1) 4 [hasValue]= bool(true) } } [tagContent]= string(8) Abegweit [tag]= string(6) option [showEndTag]= bool(false) [children]= array(0) { } } O_o Just to confirm, I checked a test instance of the site on CentOS 4, with PHP 4.3, and I get one bool(true) for each option - not two as is happening with PHP 5.2. -kgd (I haven't worked with PHP for quite a while, and I never really spent a lot of time getting deep into complex data structures and object hierarchies like this when I was using it. But this behaviour does NOT match what I know of passing values and object references around in any other language.) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String passed to object constructor turning into aninstance of that object?
On Thu, Dec 16, 2010 at 3:21 PM, Kris Deugau kdeu...@vianet.ca 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) And dumping $name itself gives: string(8) Abegweit object(SelectBoxOption)#65 (5) { [attributes]= array(1) { [0]= object(TagAttribute)#66 (3) { [name]= string(5) value [value]= string(1) 4 [hasValue]= bool(true) } } [tagContent]= string(8) Abegweit [tag]= string(6) option [showEndTag]= bool(false) [children]= array(0) { } } O_o Just to confirm, I checked a test instance of the site on CentOS 4, with PHP 4.3, and I get one bool(true) for each option - not two as is happening with PHP 5.2. probly something screwy going on w/ the old style of naming constructors. 2 things, 1. can you post the Tag constructor as it reads now? 2. try modifying Tag SelectBoxOption to have __construct() instead of Tag() SelectBoxOption(), then call parent::__construct() from inside of SelectBoxOption::__construct(); see if that clears up your problem under 5.2 (read: this will only be a partial solution as it only addresses one child of Tag). -kgd (I haven't worked with PHP for quite a while, and I never really spent a lot of time getting deep into complex data structures and object hierarchies like this when I was using it. But this behaviour does NOT match what I know of passing values and object references around in any other language.) Probly because the term 'reference' in php means something rather different than it does in say java for example.
Re: [PHP] String passed to object constructor turning into aninstance of that object?
It's acting as if Tag's constructor a) declares $name as a reference using $name, and b) is assigning itself ($this) to $name for some (probably bad) reason. That's the only way I can see that $name inside SelectBoxOption's constructor could change from a string to an object. A peek at Tag's constructor could really clear things up. David
Re: [PHP] String passed to object constructor turning into aninstance of that object?
On Thu, Dec 16, 2010 at 3:21 PM, Kris Deugau kdeu...@vianet.ca 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