Building a Base Class
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Building a Base Class
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.
(#10850)
Re: Building a Base Class
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.
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Building a Base Class
Deja vu 
Re: Building a Base Class
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
Code: Select all
public function toArray()
{
return ArrayIterator( $this->someData);
}- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Building a Base Class
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.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
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 ...
(#10850)
Re: Building a Base Class
Base classes..
I used to have some! They were called functions.php and included massive amounts of functions that often weren't needed.
I used to have some! They were called functions.php and included massive amounts of functions that often weren't needed.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Re: Building a Base Class
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
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
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:
Code: Select all
$myobj = new MyClass();
foreach ($myObj as $foo) {
// etc
}Code: Select all
$myobj = new MyClass();
echo $myObj;
Re: Building a Base Class
So there was this train riding along the rails when suddenly... 
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Building a Base Class
True... though technically your question was answered in the first postTheory? wrote:So there was this train riding along the rails when suddenly...
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Building a Base Class
Yes. I just threw the idea out there and ended up getting grilled about it.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:
PS - In the process, I did discover that constructor method way to use ArrayIterator which I may find a use for someday...
(#10850)
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Building a Base Class
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.Theory? wrote:So there was this train riding along the rails when suddenly...
(#10850)
Re: Building a Base Class
Seearborint wrote:PS - In the process, I did discover that constructor method way to use ArrayIterator which I may find a use for someday...