Page 2 of 3

Re: Building a Base Class

Posted: Tue Jan 13, 2009 7:31 pm
by Christopher
Yes immutable, like __toString() it would just supply a snapshot. It is really a type specific accessor. Just like __toString() is like a getAsString() method, __toArray() would be like a getAsArray() method -- but the magic functions are called with the object is uses as a specific variable type.

Re: Building a Base Class

Posted: Tue Jan 13, 2009 7:49 pm
by josh
Naw you got it all wrong. Take a look at the ArrayIterator package.

You just do $iterableObj = new ArrayIterator( array( 'one fish', 'two fish' ) );

There's other iterators like the one you posted which allow / require overriding pointers and such, for custom iteration logic. That's why it wouldn't be the same thing as a __toArray() method.

Re: Building a Base Class

Posted: Tue Jan 13, 2009 8:47 pm
by Christopher
:roll:

Re: Building a Base Class

Posted: Tue Jan 13, 2009 8:57 pm
by Chris Corbyn
Deja vu :twisted:

Re: Building a Base Class

Posted: Tue Jan 13, 2009 9:21 pm
by josh
Well I'm saying you don't have to hard code a class.

Code: Select all

public function toArray()
{
  return ArrayIterator( $this->someData);
}
But how would you make this into a language feature ( or framework )? Would it just iterate public properties. My fundamental point I guess is classes are not hash maps

Re: Building a Base Class

Posted: Tue Jan 13, 2009 10:44 pm
by Christopher
:)
jshpro2 wrote:Well I'm saying you don't have to hard code a class.

But how would you make this into a language feature ( or framework )? Would it just iterate public properties. My fundamental point I guess is classes are not hash maps
Yeah, I think we showed some of the ways that we could use ArrayIterator to achieve various kinds of functionality. And, I think I showed that it is a good amount of code if you want to implement a ArrayIterator. And I think my constructor example and yours show other ways to do it but they are limiting because you either have to inherit a class or in your example can't actually create a custom class, or some other problem. And yeah, __toArray() would be immutable like __toString(). But you could iterate anything __toArray() returned -- not just a property.

I was just saying that it would be a handy addition to the language for have a really easy to achieve things like foreach($obj as $value) ... maybe the PHP Group will implement it some day ...

Re: Building a Base Class

Posted: Tue Jan 13, 2009 10:47 pm
by s.dot
Base classes..

I used to have some! They were called functions.php and included massive amounts of functions that often weren't needed. :lol:

Re: Building a Base Class

Posted: Wed Jan 14, 2009 3:47 am
by josh
Eh I'm still not understanding but it doesn't matter hah, since I doubt they're implementing it anytime soon with all the updates for unicode they're doing in php6 they've probably got their hands full hah

Re: Building a Base Class

Posted: Wed Jan 14, 2009 3:51 am
by Eran
I think what arborint is saying is that allowing to declare a custom toArray conversion would be useful. Not an automatic iteration on member properties, but custom logic that you can add to any class you want, that determines what happens what an object instance is cast to an array.

Re: Building a Base Class

Posted: Wed Jan 14, 2009 8:43 am
by Jenk
So aborint, are you referring to the default behaviour, and what you'd like to change, in the following example?

Code: Select all

 
$myobj = new MyClass();
 
foreach ($myObj as $foo) {
  // etc
}
The above is what __toArray() would used for.. just like __toString() would be used in the following:

Code: Select all

$myobj = new MyClass();
 
echo $myObj;
 

Re: Building a Base Class

Posted: Wed Jan 14, 2009 9:30 am
by Theory?
So there was this train riding along the rails when suddenly... :wink:

Re: Building a Base Class

Posted: Wed Jan 14, 2009 10:08 am
by allspiritseve
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 ;)

Re: Building a Base Class

Posted: Wed Jan 14, 2009 11:36 am
by Christopher
Jenk wrote:So aborint, are you referring to the default behaviour, and what you'd like to change, in the following example?

The above is what __toArray() would used for.. just like __toString() would be used in the following:
Yes. I just threw the idea out there and ended up getting grilled about it. :) I was just saying that if they added __toArray() it would make it a lot easier than implementing ArrayIterator because for most cases you are looping through the whole array as read-only. Like __toString(), the implementation would be really easy and would not require inheritance. I have no idea whether the PHP Group has any discussion about adding it to the language.

PS - In the process, I did discover that constructor method way to use ArrayIterator which I may find a use for someday...

Re: Building a Base Class

Posted: Wed Jan 14, 2009 11:37 am
by Christopher
Theory? wrote:So there was this train riding along the rails when suddenly... :wink:
I don't think anyone thought a base class was a good idea. And many, like myself, have tried it. If you read the responses though, you will get some good insights about OO design.

Re: Building a Base Class

Posted: Wed Jan 14, 2009 12:44 pm
by josh
arborint wrote:PS - In the process, I did discover that constructor method way to use ArrayIterator which I may find a use for someday...
See :-D Accusing me of causing deja vu... sheeeesh