Building a Base Class

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Building a Base Class

Post by josh »

By the way SPL already does what you said. There's more to it then what's documented. If you make your class implement IteratorAggregate you then override getIterator(). This does exactly what you describe with your __toArray() method. Once a class implements IteratorAggregate you should be able to foreach it. The foreach will call getIterator behind the scenes. I'd much rather have this as an interface then something that's built into the "base class", I'd rather see them abolish __toString and make that an interface like SPL as well.
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Re: Building a Base Class

Post by Theory? »

allspiritseve wrote:
Theory? wrote:So there was this train riding along the rails when suddenly... :wink:
True... though technically your question was answered in the first post ;)
Oh, I know. I'm just being a funny-man :mrgreen:
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Building a Base Class

Post by alex.barylski »

I'm only half fast following this thread...but the __toArray() idea...

Can you not already iterate public members?

Code: Select all

foreach($object as $value){
...
}
volomike (I think it was him) sent me an interesting tip on my blog.

Apparently:

Code: Select all

$object = (object)$array;
I wonder if the reverse is also true?

Cheers,
Alex
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Building a Base Class

Post by Christopher »

jshpro2 wrote:By the way SPL already does what you said. There's more to it then what's documented. If you make your class implement IteratorAggregate you then override getIterator(). This does exactly what you describe with your __toArray() method. Once a class implements IteratorAggregate you should be able to foreach it. The foreach will call getIterator behind the scenes. I'd much rather have this as an interface then something that's built into the "base class", I'd rather see them abolish __toString and make that an interface like SPL as well.
I have already said about 100 times that SPL already does this in several ways. I even showed code on how to do it. I have gone over the differences in implementations several times too, discussing their stengths and weaknesses. I understand all of that -- even the IteratorAggregate solution you mentioned. I just thought that adding a __toString() like method for arrays would allow you to (with minimal code and no inheritance) use an object as an immutable array. However you seem to think this idea (which may never be added to PHP) is so bad that not only should it never be implemented, but that __toString() should also be abolished. I am starting to get the impression that we're not finding much common ground here...
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Building a Base Class

Post by alex.barylski »

I like those magic functions, although I find it very rare they come in handy.

__toString() is useful in a base class that is only used in debug environments, having it basically do:

Code: Select all

function __toString()
{
  echo '<pre>';
  print_r($this);
}
Makes bebugging a little easier as all I have to do now is:

Code: Select all

$obj = new Object();
 
echo $obj; // Get debug information
My question though, can you not already iterate public members using a foreach loop? If an object overrode __toArray() what would it return, an array? This would be so you could iterate object properties like a standard array???

Cheers,
Alex
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Building a Base Class

Post by Christopher »

PCSpectra wrote:My question though, can you not already iterate public members using a foreach loop? If an object overrode __toArray() what would it return, an array? This would be so you could iterate object properties like a standard array???
Yeah sure you sometime might want that, but we covered all of these combination previously. Getting an array of the public properties is useful for a specific subset of Value Objects. But there are lots of cases, such as collections, where you want to return an arbitrary array when the object is used in context as an array. Examples might be a DB Row object, or a DaysOfWeek object where you could set the name length and language. With neither of those would you want an array of all the public properties. I think wanting an arbitrary array is much more common than wanting all the public properties.
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Building a Base Class

Post by josh »

I dont get what __toArray would do that SPL doesn't, it would just be renaming the method and taking away the "implements" part of it, but nevermind.

I actually found a bug in Magento today they use __toString with arguments. In php 5.3 they add a fatal error for this, they require 5.2. So that means if you modified their code base you're now officially coupled to php 5.2 for life, yay!

I dont have anything against these features, I just think they're so inconsistent. The "implements" makes it slightly more explicit.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Building a Base Class

Post by Christopher »

Yeah.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Building a Base Class

Post by alex.barylski »

I actually found a bug in Magento today they use __toString with arguments.
You sound surprised? :lol: :D

I have only glanced at that codebase and cringed when I did...where are the project managers....don't they do daily code reviews quickly to ensure quality?

I mean, even if your average developer hammers out 500 or maybe 1000 lines a day, surely an senior developer could spend 15-30 minutes just quickly eyeballing the code to ensure best practices were followed and bad practices were avoided. I do that daily myself at days end, just to double check my own work.

Shows to go ya...even altruistic motives (as opposed to commercial motives) don't gaurnatee quality source code.
Post Reply