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

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 »

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)
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 »

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.
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 »

:roll:
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Building a Base Class

Post by Chris Corbyn »

Deja vu :twisted:
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 »

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
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: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 ...
(#10850)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Building a Base Class

Post 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:
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.
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 »

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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Building a Base Class

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Building a Base Class

Post 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;
 
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Re: Building a Base Class

Post by Theory? »

So there was this train riding along the rails when suddenly... :wink:
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Building a Base Class

Post 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 ;)
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 »

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...
(#10850)
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 »

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.
(#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 »

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
Post Reply