Hi guys,
What do you think about the possibility to support ArrayObject
instances in array_* functions?
If you all agreed on this, I can definately help to complete the
patch, but I need some initial guidance to finish at least the first
function.
Cheers,
--
Guilherme Blanco - Web Developer
CBC - Certified Bindows Consultant
Cell Phone: +55 (16) 9215-8480
MSN: guilhermeblanco@hotmail.com
URL: http://blog.bisna.com
São Paulo - SP/Brazil
What do you think about the possibility to support ArrayObject
instances in array_* functions?
If you all agreed on this, I can definately help to complete the
patch, but I need some initial guidance to finish at least the first
function.
I think it's a marvelous idea :)
I'm attaching Travis Swicegood to this as he had cooked a very simple
but effective patch for that at #tek09
--
Slan,
David
2009/7/30 David Coallier davidc@php.net
What do you think about the possibility to support ArrayObject
instances in array_* functions?
If you all agreed on this, I can definately help to complete the
patch, but I need some initial guidance to finish at least the first
function.I think it's a marvelous idea :)
I'm attaching Travis Swicegood to this as he had cooked a very simple
but effective patch for that at #tek09
I would also find it very useful.
What do you think about the possibility to support ArrayObject
instances in array_* functions?
If you all agreed on this, I can definately help to complete the
patch, but I need some initial guidance to finish at least the first
function.
In general I'm for bringing Iterators/ArrayObject/.. on a line with
arrays but please don't simply patch some functions but let's try to
find a as consistent as possible global solution.
Questions included there are of these kinds (just examples)
- is that specific to ArrayObject or do we "need" interfaces like
"Sortable" or "Shuffable" - What should stuff like aray_merge(ArrayObject, array) do
- Should we do this globally? (should ldap_set_option()
allow an ArrayObject as 3rd param?)
Especially the latter shows that a proper solution might be not to patch
individual functions but to introduce some form of abstraction from
HashTables for "array operations" and then use that interface.
Simply extending a few functions will end in a mess but looking around
people seem to love these structures so improving them is good.
johannes
2009/7/30 Johannes Schlüter johannes@schlueters.de:
What do you think about the possibility to support ArrayObject
instances in array_* functions?
If you all agreed on this, I can definately help to complete the
patch, but I need some initial guidance to finish at least the first
function.In general I'm for bringing Iterators/ArrayObject/.. on a line with
arrays but please don't simply patch some functions but let's try to
find a as consistent as possible global solution.Questions included there are of these kinds (just examples)
- is that specific to ArrayObject or do we "need" interfaces like
"Sortable" or "Shuffable"- What should stuff like aray_merge(ArrayObject, array) do
- Should we do this globally? (should ldap_set_option()
allow an ArrayObject as 3rd param?)Especially the latter shows that a proper solution might be not to patch
individual functions but to introduce some form of abstraction from
HashTables for "array operations" and then use that interface.Simply extending a few functions will end in a mess but looking around
people seem to love these structures so improving them is good.
agreed.
the proper solution would be to define several interfaces, make
functions accept objects of these interfaces and then add some
"hacks", which would pretend, that array() implements these.
--
Alexey Zakhlestin
http://www.milkfarmsoft.com/
Sent from St Petersburg, Russian Federation
I'm trying to avoid a "globally solution" on first instance to avoid a
great impact on language.
If you wanna ask me what's the global solution, I'll suggest a
Collection like implementation for all array functions.
http://en.wikipedia.org/wiki/Collection_%28computing%29
http://trac.doctrine-project.org/browser/trunk/lib/Doctrine/Common/Collections
Since it's a too much impactable code, I prefer to go straight at the
first glance to support basic operations, treating ArrayObjects as
normal arrays.
This would lead to a simpler implementation. So, here are the answers
of your questions:
- is that specific to ArrayObject or do we "need" interfaces like "Sortable" or "Shuffable"
All Interfaces that can be treated as normal arrays (implements
ArrayObject) should be supported.
Why not ArrayAccess on first stage? I do not consider it right now
because it'll require the method getArrayCopy() to be moved from
ArrayObject to ArrayAccess.
- What should stuff like array_merge(ArrayObject, array) do
It should return an array, just like the current function prototype defines
- Should we do this globally? (should ldap_set_option() allow an ArrayObject as 3rd param?)
This can be obviously be considered... but at the first step, let's be
strict to the plan an only support array operations.
Basically what would be the approach I'm looking for?
For all array_* functions, inspect if given parameter is an array or
an instance of ArrayObject.
If instance of ArrayObject, call ArrayObject::getArrayCopy() and
proceed normally with execution of each array_* function. It can't be
that hard! =)
Cheers,
2009/7/30 Alexey Zakhlestin indeyets@gmail.com:
2009/7/30 Johannes Schlüter johannes@schlueters.de:
What do you think about the possibility to support ArrayObject
instances in array_* functions?
If you all agreed on this, I can definately help to complete the
patch, but I need some initial guidance to finish at least the first
function.In general I'm for bringing Iterators/ArrayObject/.. on a line with
arrays but please don't simply patch some functions but let's try to
find a as consistent as possible global solution.Questions included there are of these kinds (just examples)
- is that specific to ArrayObject or do we "need" interfaces like
"Sortable" or "Shuffable"- What should stuff like aray_merge(ArrayObject, array) do
- Should we do this globally? (should ldap_set_option()
allow an ArrayObject as 3rd param?)Especially the latter shows that a proper solution might be not to patch
individual functions but to introduce some form of abstraction from
HashTables for "array operations" and then use that interface.Simply extending a few functions will end in a mess but looking around
people seem to love these structures so improving them is good.agreed.
the proper solution would be to define several interfaces, make
functions accept objects of these interfaces and then add some
"hacks", which would pretend, that array() implements these.--
Alexey Zakhlestin
http://www.milkfarmsoft.com/
Sent from St Petersburg, Russian Federation
--
Guilherme Blanco - Web Developer
CBC - Certified Bindows Consultant
Cell Phone: +55 (16) 9215-8480
MSN: guilhermeblanco@hotmail.com
URL: http://blog.bisna.com
São Paulo - SP/Brazil
I'm trying to avoid a "globally solution" on first instance to avoid a
great impact on language.
If you wanna ask me what's the global solution, I'll suggest a
Collection like implementation for all array functions.
Define "all array functions" - What you're ending up with is that one
has to always look up whether a given function takes an Object of some
kind or not.
I'd rather have a "yes, unless there's something good reason not to"
This would lead to a simpler implementation. So, here are the answers
of your questions:
As said I didn't mean them as actual questions, but pointers to possible
problems in regards to a clean design.
- is that specific to ArrayObject or do we "need" interfaces like "Sortable" or "Shuffable"
All Interfaces that can be treated as normal arrays (implements
ArrayObject) should be supported.
ArrayObject is no Interface but a concrete class that means everybody
extending from there creates a is-a relationship which might be quite
limiting.
Why not ArrayAccess on first stage? I do not consider it right now
because it'll require the method getArrayCopy() to be moved from
ArrayObject to ArrayAccess.
- What should stuff like array_merge(ArrayObject, array) do
It should return an array, just like the current function prototype defines
That was a simple case, agreed, but thee might be cases where it's
probably not that clear what type should be used so again this should be
seen globally t come up with a as consistent solution as possible.
- Should we do this globally? (should ldap_set_option() allow an ArrayObject as 3rd param?)
This can be obviously be considered... but at the first step, let's be
strict to the plan an only support array operations.
Why isn't that an "array operation"? Where's the strict line? Any
function prefixed with array_? - Then sort()
would be ignored. Anything
in ext/standard using arrays? Then don't forget str_replace and other
functions ... and that's not clear for users. So where's the strict
line?
Basically what would be the approach I'm looking for?
For all array_* functions, inspect if given parameter is an array or
an instance of ArrayObject.
If instance of ArrayObject, call ArrayObject::getArrayCopy() and
proceed normally with execution of each array_* function. It can't be
that hard! =)
By using getArrayCopy you are introducing quite possibly a huge
performance penalty for using ArrayObject ... creating a copy of the
HashTable just for throwing it away a moment later.
Yes, such an implementation would be trivial but I don't think it's what
we'd want.
johannes
Until now I tried to find the easiest possible solution.
Currently we have two paths to chose:
1- A class that implements ArrayAccess must be useable everywhere an
"array" is useable;
2- ArrayAccess needs to be expanded with methods for all the array
functions and ArrayObject needs to implement them;
I was choosing the approach 1 since it was easier and with less impact
on source.
Since you want something better designed, we'll have to go on 2nd option.
Please notice I do not want to override our current implementation of
arrays in PHP, but it will bring 2 main advantages:
A- It does not require hacking PHP array functions
B- It gives you an OO interface
The idea then was to keep it as an alternative, not a replacement.
What's the scope them, you may ask me. Here it is:
http://www.php.net/manual/en/book.array.php
If this implementation covers these functions, I'll be nice.
How would it be achieved?
$coll->sort(); instead of sort($coll);
$coll2 = $coll->reverse(); instead of $coll2 = array_reverse($coll);
Is this better now?
Cheers,
2009/7/30 Johannes Schlüter johannes@schlueters.de:
I'm trying to avoid a "globally solution" on first instance to avoid a
great impact on language.
If you wanna ask me what's the global solution, I'll suggest a
Collection like implementation for all array functions.Define "all array functions" - What you're ending up with is that one
has to always look up whether a given function takes an Object of some
kind or not.I'd rather have a "yes, unless there's something good reason not to"
This would lead to a simpler implementation. So, here are the answers
of your questions:As said I didn't mean them as actual questions, but pointers to possible
problems in regards to a clean design.
- is that specific to ArrayObject or do we "need" interfaces like "Sortable" or "Shuffable"
All Interfaces that can be treated as normal arrays (implements
ArrayObject) should be supported.ArrayObject is no Interface but a concrete class that means everybody
extending from there creates a is-a relationship which might be quite
limiting.Why not ArrayAccess on first stage? I do not consider it right now
because it'll require the method getArrayCopy() to be moved from
ArrayObject to ArrayAccess.
- What should stuff like array_merge(ArrayObject, array) do
It should return an array, just like the current function prototype defines
That was a simple case, agreed, but thee might be cases where it's
probably not that clear what type should be used so again this should be
seen globally t come up with a as consistent solution as possible.
- Should we do this globally? (should ldap_set_option() allow an ArrayObject as 3rd param?)
This can be obviously be considered... but at the first step, let's be
strict to the plan an only support array operations.Why isn't that an "array operation"? Where's the strict line? Any
function prefixed with array_? - Thensort()
would be ignored. Anything
in ext/standard using arrays? Then don't forget str_replace and other
functions ... and that's not clear for users. So where's the strict
line?Basically what would be the approach I'm looking for?
For all array_* functions, inspect if given parameter is an array or
an instance of ArrayObject.
If instance of ArrayObject, call ArrayObject::getArrayCopy() and
proceed normally with execution of each array_* function. It can't be
that hard! =)By using getArrayCopy you are introducing quite possibly a huge
performance penalty for using ArrayObject ... creating a copy of the
HashTable just for throwing it away a moment later.Yes, such an implementation would be trivial but I don't think it's what
we'd want.johannes
--
Guilherme Blanco - Web Developer
CBC - Certified Bindows Consultant
Cell Phone: +55 (16) 9215-8480
MSN: guilhermeblanco@hotmail.com
URL: http://blog.bisna.com
São Paulo - SP/Brazil
Hi!
1- A class that implements ArrayAccess must be useable everywhere an
"array" is useable;
The problem is that many functions dealing with arrays do not make sense
with generic object. For others, it may make sense in theory, but not be
practical - e.g., suppose you have ArrayAccess object referring to a
database field, how easy would be to make shuffle()
work on it? How
practical would it be?
2- ArrayAccess needs to be expanded with methods for all the array
functions and ArrayObject needs to implement them;
All 70+ of them? That'd be one fat interface.
How would it be achieved?
$coll->sort(); instead of sort($coll);
$coll2 = $coll->reverse(); instead of $coll2 = array_reverse($coll);
Note that this also eats up a lot of useful names, and makes you
implement functions that you would never use, such as picking a random
element or finding difference between arrays.
I think all array operations should be divided into following classes:
- Basic ones, probably ArrayAccess/Iterator interface should be enough
- Compound ones implemented on top of basic ones (i.e. array_sum is a
combination of iteration and +, etc.) - Additional operations non-reducible to (1), like shift/unsift or
exotic ones likearray_change_key_case()
.
First two classes are easy to handle, the last one should be handled
separately on per-case basis.
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
Hi Stan,
I don't really know which implementation is preferrable.
I just dropped the idea here first. Once decided, I can write a detailed RFC.
With some initial help, I can work on the patch to support it. I'm not
expert in internals, so some help will be surely needed.
I'm totally opened to suggestions. If chosen the approach 2, I'd like
to consider naming that Roman said: SplCollection and SplArray.
We can also merge with your idea to split into small subsets.... but
surely the array OO native implementation (Collection, SplArray, etc)
must implement all of them.
So... what do I need to do to move this idea forward? Open a poll
about which approach should be considered for RFC?
Cheers,
Hi!
1- A class that implements ArrayAccess must be useable everywhere an
"array" is useable;The problem is that many functions dealing with arrays do not make sense
with generic object. For others, it may make sense in theory, but not be
practical - e.g., suppose you have ArrayAccess object referring to a
database field, how easy would be to makeshuffle()
work on it? How
practical would it be?2- ArrayAccess needs to be expanded with methods for all the array
functions and ArrayObject needs to implement them;All 70+ of them? That'd be one fat interface.
How would it be achieved?
$coll->sort(); instead of sort($coll);
$coll2 = $coll->reverse(); instead of $coll2 = array_reverse($coll);Note that this also eats up a lot of useful names, and makes you implement
functions that you would never use, such as picking a random element or
finding difference between arrays.I think all array operations should be divided into following classes:
- Basic ones, probably ArrayAccess/Iterator interface should be enough
- Compound ones implemented on top of basic ones (i.e. array_sum is a
combination of iteration and +, etc.)- Additional operations non-reducible to (1), like shift/unsift or exotic
ones likearray_change_key_case()
.First two classes are easy to handle, the last one should be handled
separately on per-case basis.Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
--
Guilherme Blanco - Web Developer
CBC - Certified Bindows Consultant
Cell Phone: +55 (16) 9215-8480
MSN: guilhermeblanco@hotmail.com
URL: http://blog.bisna.com
São Paulo - SP/Brazil
Hi,
I like this idea very much, too but I do see the problems in doing it
correctly.
From my point of view the problem looks as follows:
PHP arrays are a wonderful universal construct but it is problematic
to use them in scenarios where you dont want to bind directly to the
single, available implementation, in order to allow a third party to
choose an implementation, as long as it fulfills the "contract" of an
array.
So rather than directly binding to the implementation it would be
useful to be able to instead just bind to a contract/interface.
That would be possible if either:
a) A class that implements ArrayAccess is useable everywhere an
"array" is useable ("array implements ArrayAccess" so to speak).
or
b) ArrayAccess is expanded and turned into a rich interface with usual
array/collection methods, many of these being direct counterparts of
array functions. Subsequently ArrayObject would need to implement
these methods, probably internally simply using the array functions on
the internal hashmap (assuming there is an internal hashmap).
I see that a) is very hard to do right and I see that b) is
problematic because of backwards compatibility and because ArrayAccess
would really no longer be the right name if it is turned into a rich
interface. A last alternative would be b) but with a new rich
interface (SplCollection?) and a new default implementation
(SplArray?) of that interface.
While a) may probably fit much better into the language because we do
not "duplicate" array functions as methods on an alternative OO API,
option b) (with the new interface+class) would probably be much easier
to implement because it keeps the current "separation" of array <->
ArrayAccess/ArrayObject/... without the need to hack any of the
existing functions.
Just my 2 cents, either way, some progress in that direction, whether
a) or b) would be very useful I think.
Thanks for reading!
Roman
2009/7/30 Alexey Zakhlestin indeyets@gmail.com
2009/7/30 Johannes Schlüter johannes@schlueters.de:
What do you think about the possibility to support ArrayObject
instances in array_* functions?
If you all agreed on this, I can definately help to complete the
patch, but I need some initial guidance to finish at least the first
function.In general I'm for bringing Iterators/ArrayObject/.. on a line with
arrays but please don't simply patch some functions but let's try to
find a as consistent as possible global solution.Questions included there are of these kinds (just examples)
- is that specific to ArrayObject or do we "need" interfaces like
"Sortable" or "Shuffable"- What should stuff like aray_merge(ArrayObject, array) do
- Should we do this globally? (should ldap_set_option()
allow an ArrayObject as 3rd param?)Especially the latter shows that a proper solution might be not to patch
individual functions but to introduce some form of abstraction from
HashTables for "array operations" and then use that interface.Simply extending a few functions will end in a mess but looking around
people seem to love these structures so improving them is good.agreed.
the proper solution would be to define several interfaces, make
functions accept objects of these interfaces and then add some
"hacks", which would pretend, that array() implements these.
I would vote to extend this concept to user-land functions as well if at all
possible. It would make my life easier to not care about the implementation
of a set of data so long as it conformed to an interface.
--
-Nathan Gordon
If the database server goes down and there is no code to hear it, does it
really go down?
<esc>:wq<CR