Req #38508 [Com]: Addition of Magic __toArray() function

2013-10-05 Thread lukas at lu-x dot me
Edit report at https://bugs.php.net/bug.php?id=38508edit=1

 ID: 38508
 Comment by: lukas at lu-x dot me
 Reported by:doublecompile at gmail dot com
 Summary:Addition of Magic __toArray() function
 Status: Closed
 Type:   Feature/Change Request
 Package:Feature/Change Request
 PHP Version:*
 Block user comment: N
 Private report: N

 New Comment:

I can only +1 the opinion from the last comment.
Magic methods make PHP so powerful. So why limit it to string casting?

The creator of the class should decide what values to return in which case - 
the user could just use the object in different ways without thinking about 
what methods would return what. It would all work automatically - using an 
array function would ask the object to return an array, using a string function 
would ask to return a string etc.

So please implement magic methods for all data types in PHP - or, if that adds 
more complexity, add interfaces for that - the possibility to define custom 
conversion rules just has to exist!


Previous Comments:

[2013-05-24 03:39:23] rich dot remer at gmail dot com

I think the main benefit it offers is the ability to control what happens 
during 
a cast operation.  Right now, casting simple scalar values or NULL to an array 
works as expected.

While it's possible to cast an object to an array, the semantics of what should 
happen in this situation are not nearly as clear.  This really should be 
controlled by the class.


[2013-03-07 12:19:14] ante at caan dot si

Hi guys. I'm dragging this out from the History. I think this is a great 
suggestion as I use a lot of object that implement ArrayAccess so doing this

$someArray = (array) $obj;

that calls $obj-__toArray() would be a GREAT addition to PHP logic.


[2012-03-14 18:55:00] erck0006 at gmail dot com

// MY APPLICATION'S INTERIM SOLUTION (**UPDATE**)
$customers = new Customers();
$customers = $customers-__toArray(); // THIS LINE IS THE **UPDATED** LINE 
NEEDED IN ORDER TO AVOID TRIGGERING THE FOLLOWING ERROR ON THE FOLLOWING LINE: 
Only variables should be passed by reference
$lastCustomer = array_pop($customers); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since the 
previous line did not fail since I explicitly called a custom method named 
__toArray() first


[2012-03-14 18:37:23] erck0006 at gmail dot com

?php
// AS-IS
$customers = new Customers();
$lastCustomer = array_pop($customers); // FAIL: array_pop() expects parameter 1 
to be array, object given
echo $lastCustomer-getName(); // execution never reaches this line

// MY APPLICATION'S INTERIM SOLUTION
$customers = new Customers();
$lastCustomer = array_pop($customers-__toArray()); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since the 
previous line did not fail since I explicitly called a custom method named 
__toArray() first

// TO-BE
$customers = new Customers();
$lastCustomer = array_pop($customers); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since 
array_pop() calls __toArray() internally before failing


[2012-02-27 21:27:32] john at john dot com

this magic function would be great to have




The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

https://bugs.php.net/bug.php?id=38508


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=38508edit=1


Req #38508 [Com]: Addition of Magic __toArray() function

2013-05-23 Thread rich dot remer at gmail dot com
Edit report at https://bugs.php.net/bug.php?id=38508edit=1

 ID: 38508
 Comment by: rich dot remer at gmail dot com
 Reported by:doublecompile at gmail dot com
 Summary:Addition of Magic __toArray() function
 Status: Closed
 Type:   Feature/Change Request
 Package:Feature/Change Request
 PHP Version:*
 Block user comment: N
 Private report: N

 New Comment:

I think the main benefit it offers is the ability to control what happens 
during 
a cast operation.  Right now, casting simple scalar values or NULL to an array 
works as expected.

While it's possible to cast an object to an array, the semantics of what should 
happen in this situation are not nearly as clear.  This really should be 
controlled by the class.


Previous Comments:

[2013-03-07 12:19:14] ante at caan dot si

Hi guys. I'm dragging this out from the History. I think this is a great 
suggestion as I use a lot of object that implement ArrayAccess so doing this

$someArray = (array) $obj;

that calls $obj-__toArray() would be a GREAT addition to PHP logic.


[2012-03-14 18:55:00] erck0006 at gmail dot com

// MY APPLICATION'S INTERIM SOLUTION (**UPDATE**)
$customers = new Customers();
$customers = $customers-__toArray(); // THIS LINE IS THE **UPDATED** LINE 
NEEDED IN ORDER TO AVOID TRIGGERING THE FOLLOWING ERROR ON THE FOLLOWING LINE: 
Only variables should be passed by reference
$lastCustomer = array_pop($customers); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since the 
previous line did not fail since I explicitly called a custom method named 
__toArray() first


[2012-03-14 18:37:23] erck0006 at gmail dot com

?php
// AS-IS
$customers = new Customers();
$lastCustomer = array_pop($customers); // FAIL: array_pop() expects parameter 1 
to be array, object given
echo $lastCustomer-getName(); // execution never reaches this line

// MY APPLICATION'S INTERIM SOLUTION
$customers = new Customers();
$lastCustomer = array_pop($customers-__toArray()); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since the 
previous line did not fail since I explicitly called a custom method named 
__toArray() first

// TO-BE
$customers = new Customers();
$lastCustomer = array_pop($customers); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since 
array_pop() calls __toArray() internally before failing


[2012-02-27 21:27:32] john at john dot com

this magic function would be great to have


[2011-05-23 23:32:13] spark at limao dot com dot br

Because of the magic __toString I was able to write a String class with the 
same methods as the Java String class.
I was trying to write an Array class to map the one in Adobe AS3, but then I 
came across the lack of __toArray.
I don't want a __toArray, or even a toString
I want classes with reasonable methods and not lots of functions with no naming 
rules nor parameter order standard.
I know it may sound rude but that's how I feel when I see a language getting so 
far and still not following any coding convetion.




The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

https://bugs.php.net/bug.php?id=38508


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=38508edit=1


Req #38508 [Com]: Addition of Magic __toArray() function

2013-03-07 Thread ante at caan dot si
Edit report at https://bugs.php.net/bug.php?id=38508edit=1

 ID: 38508
 Comment by: ante at caan dot si
 Reported by:doublecompile at gmail dot com
 Summary:Addition of Magic __toArray() function
 Status: Closed
 Type:   Feature/Change Request
 Package:Feature/Change Request
 PHP Version:*
 Block user comment: N
 Private report: N

 New Comment:

Hi guys. I'm dragging this out from the History. I think this is a great 
suggestion as I use a lot of object that implement ArrayAccess so doing this

$someArray = (array) $obj;

that calls $obj-__toArray() would be a GREAT addition to PHP logic.


Previous Comments:

[2012-03-14 18:55:00] erck0006 at gmail dot com

// MY APPLICATION'S INTERIM SOLUTION (**UPDATE**)
$customers = new Customers();
$customers = $customers-__toArray(); // THIS LINE IS THE **UPDATED** LINE 
NEEDED IN ORDER TO AVOID TRIGGERING THE FOLLOWING ERROR ON THE FOLLOWING LINE: 
Only variables should be passed by reference
$lastCustomer = array_pop($customers); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since the 
previous line did not fail since I explicitly called a custom method named 
__toArray() first


[2012-03-14 18:37:23] erck0006 at gmail dot com

?php
// AS-IS
$customers = new Customers();
$lastCustomer = array_pop($customers); // FAIL: array_pop() expects parameter 1 
to be array, object given
echo $lastCustomer-getName(); // execution never reaches this line

// MY APPLICATION'S INTERIM SOLUTION
$customers = new Customers();
$lastCustomer = array_pop($customers-__toArray()); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since the 
previous line did not fail since I explicitly called a custom method named 
__toArray() first

// TO-BE
$customers = new Customers();
$lastCustomer = array_pop($customers); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since 
array_pop() calls __toArray() internally before failing


[2012-02-27 21:27:32] john at john dot com

this magic function would be great to have


[2011-05-23 23:32:13] spark at limao dot com dot br

Because of the magic __toString I was able to write a String class with the 
same methods as the Java String class.
I was trying to write an Array class to map the one in Adobe AS3, but then I 
came across the lack of __toArray.
I don't want a __toArray, or even a toString
I want classes with reasonable methods and not lots of functions with no naming 
rules nor parameter order standard.
I know it may sound rude but that's how I feel when I see a language getting so 
far and still not following any coding convetion.


[2006-08-20 11:12:22] he...@php.net

Why not simply have a method asArray() maybe even as par of an interface:

interface ArrayConversion
{
  function asArray();
}

See, we have __toString as it is supported in language constructs like echo, 
print and other internal functions. But we decided against an autoconversion 
for arrays already. So itwill never be supported in any language construct. 
That said there is no needed for this and nothing you would win against the 
above interface. In fact you would make it php more complex because you'd add 
just one more magic feature.




The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

https://bugs.php.net/bug.php?id=38508


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=38508edit=1


Req #38508 [Com]: Addition of Magic __toArray() function

2012-03-14 Thread erck0006 at gmail dot com
Edit report at https://bugs.php.net/bug.php?id=38508edit=1

 ID: 38508
 Comment by: erck0006 at gmail dot com
 Reported by:doublecompile at gmail dot com
 Summary:Addition of Magic __toArray() function
 Status: Closed
 Type:   Feature/Change Request
 Package:Feature/Change Request
 PHP Version:*
 Block user comment: N
 Private report: N

 New Comment:

?php
// AS-IS
$customers = new Customers();
$lastCustomer = array_pop($customers); // FAIL: array_pop() expects parameter 1 
to be array, object given
echo $lastCustomer-getName(); // execution never reaches this line

// MY APPLICATION'S INTERIM SOLUTION
$customers = new Customers();
$lastCustomer = array_pop($customers-__toArray()); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since the 
previous line did not fail since I explicitly called a custom method named 
__toArray() first

// TO-BE
$customers = new Customers();
$lastCustomer = array_pop($customers); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since 
array_pop() calls __toArray() internally before failing


Previous Comments:

[2012-02-27 21:27:32] john at john dot com

this magic function would be great to have


[2011-05-23 23:32:13] spark at limao dot com dot br

Because of the magic __toString I was able to write a String class with the 
same methods as the Java String class.
I was trying to write an Array class to map the one in Adobe AS3, but then I 
came across the lack of __toArray.
I don't want a __toArray, or even a toString
I want classes with reasonable methods and not lots of functions with no naming 
rules nor parameter order standard.
I know it may sound rude but that's how I feel when I see a language getting so 
far and still not following any coding convetion.


[2006-08-20 11:12:22] he...@php.net

Why not simply have a method asArray() maybe even as par of an interface:

interface ArrayConversion
{
  function asArray();
}

See, we have __toString as it is supported in language constructs like echo, 
print and other internal functions. But we decided against an autoconversion 
for arrays already. So itwill never be supported in any language construct. 
That said there is no needed for this and nothing you would win against the 
above interface. In fact you would make it php more complex because you'd add 
just one more magic feature.


[2006-08-19 00:40:17] doublecompile at gmail dot com

Description:

Doing this:

$newarray = (array)$object;

will take the properties of an object and assign them as the values of keys in 
an array.

As of PHP 5.2, doing this:

$stringified = (string)$object;

will call the magic __toString() function for a user-specified formatting of 
the object as a string.

It would be a great addition to call a magic __toArray() function if an object 
is cast as an array, instead of converting its public members to array 
elements.  (For instance, the class might not have public members).  Classes 
without the function could use the default method of mapping property names to 
array keys.

Just my two cents.

Reproduce code:
---
?php

class magicExample 
{
public $aoeu = htns;

function __toArray()
{
return array('foo'='bar');
}
}

$test = new magicExample();
$array = (array)$test;

print_r($array); // should show foo=bar, not aoeu=htns

?







-- 
Edit this bug report at https://bugs.php.net/bug.php?id=38508edit=1


Req #38508 [Com]: Addition of Magic __toArray() function

2012-03-14 Thread erck0006 at gmail dot com
Edit report at https://bugs.php.net/bug.php?id=38508edit=1

 ID: 38508
 Comment by: erck0006 at gmail dot com
 Reported by:doublecompile at gmail dot com
 Summary:Addition of Magic __toArray() function
 Status: Closed
 Type:   Feature/Change Request
 Package:Feature/Change Request
 PHP Version:*
 Block user comment: N
 Private report: N

 New Comment:

// MY APPLICATION'S INTERIM SOLUTION (**UPDATE**)
$customers = new Customers();
$customers = $customers-__toArray(); // THIS LINE IS THE **UPDATED** LINE 
NEEDED IN ORDER TO AVOID TRIGGERING THE FOLLOWING ERROR ON THE FOLLOWING LINE: 
Only variables should be passed by reference
$lastCustomer = array_pop($customers); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since the 
previous line did not fail since I explicitly called a custom method named 
__toArray() first


Previous Comments:

[2012-03-14 18:37:23] erck0006 at gmail dot com

?php
// AS-IS
$customers = new Customers();
$lastCustomer = array_pop($customers); // FAIL: array_pop() expects parameter 1 
to be array, object given
echo $lastCustomer-getName(); // execution never reaches this line

// MY APPLICATION'S INTERIM SOLUTION
$customers = new Customers();
$lastCustomer = array_pop($customers-__toArray()); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since the 
previous line did not fail since I explicitly called a custom method named 
__toArray() first

// TO-BE
$customers = new Customers();
$lastCustomer = array_pop($customers); // SUCCESS
echo $lastCustomer-getName(); // prints the last customer's name since 
array_pop() calls __toArray() internally before failing


[2012-02-27 21:27:32] john at john dot com

this magic function would be great to have


[2011-05-23 23:32:13] spark at limao dot com dot br

Because of the magic __toString I was able to write a String class with the 
same methods as the Java String class.
I was trying to write an Array class to map the one in Adobe AS3, but then I 
came across the lack of __toArray.
I don't want a __toArray, or even a toString
I want classes with reasonable methods and not lots of functions with no naming 
rules nor parameter order standard.
I know it may sound rude but that's how I feel when I see a language getting so 
far and still not following any coding convetion.


[2006-08-20 11:12:22] he...@php.net

Why not simply have a method asArray() maybe even as par of an interface:

interface ArrayConversion
{
  function asArray();
}

See, we have __toString as it is supported in language constructs like echo, 
print and other internal functions. But we decided against an autoconversion 
for arrays already. So itwill never be supported in any language construct. 
That said there is no needed for this and nothing you would win against the 
above interface. In fact you would make it php more complex because you'd add 
just one more magic feature.


[2006-08-19 00:40:17] doublecompile at gmail dot com

Description:

Doing this:

$newarray = (array)$object;

will take the properties of an object and assign them as the values of keys in 
an array.

As of PHP 5.2, doing this:

$stringified = (string)$object;

will call the magic __toString() function for a user-specified formatting of 
the object as a string.

It would be a great addition to call a magic __toArray() function if an object 
is cast as an array, instead of converting its public members to array 
elements.  (For instance, the class might not have public members).  Classes 
without the function could use the default method of mapping property names to 
array keys.

Just my two cents.

Reproduce code:
---
?php

class magicExample 
{
public $aoeu = htns;

function __toArray()
{
return array('foo'='bar');
}
}

$test = new magicExample();
$array = (array)$test;

print_r($array); // should show foo=bar, not aoeu=htns

?







-- 
Edit this bug report at https://bugs.php.net/bug.php?id=38508edit=1


Req #38508 [Com]: Addition of Magic __toArray() function

2012-02-27 Thread john at john dot com
Edit report at https://bugs.php.net/bug.php?id=38508edit=1

 ID: 38508
 Comment by: john at john dot com
 Reported by:doublecompile at gmail dot com
 Summary:Addition of Magic __toArray() function
 Status: Closed
 Type:   Feature/Change Request
 Package:Feature/Change Request
 PHP Version:*
 Block user comment: N
 Private report: N

 New Comment:

this magic function would be great to have


Previous Comments:

[2011-05-23 23:32:13] spark at limao dot com dot br

Because of the magic __toString I was able to write a String class with the 
same methods as the Java String class.
I was trying to write an Array class to map the one in Adobe AS3, but then I 
came across the lack of __toArray.
I don't want a __toArray, or even a toString
I want classes with reasonable methods and not lots of functions with no naming 
rules nor parameter order standard.
I know it may sound rude but that's how I feel when I see a language getting so 
far and still not following any coding convetion.


[2006-08-20 11:12:22] he...@php.net

Why not simply have a method asArray() maybe even as par of an interface:

interface ArrayConversion
{
  function asArray();
}

See, we have __toString as it is supported in language constructs like echo, 
print and other internal functions. But we decided against an autoconversion 
for arrays already. So itwill never be supported in any language construct. 
That said there is no needed for this and nothing you would win against the 
above interface. In fact you would make it php more complex because you'd add 
just one more magic feature.


[2006-08-19 00:40:17] doublecompile at gmail dot com

Description:

Doing this:

$newarray = (array)$object;

will take the properties of an object and assign them as the values of keys in 
an array.

As of PHP 5.2, doing this:

$stringified = (string)$object;

will call the magic __toString() function for a user-specified formatting of 
the object as a string.

It would be a great addition to call a magic __toArray() function if an object 
is cast as an array, instead of converting its public members to array 
elements.  (For instance, the class might not have public members).  Classes 
without the function could use the default method of mapping property names to 
array keys.

Just my two cents.

Reproduce code:
---
?php

class magicExample 
{
public $aoeu = htns;

function __toArray()
{
return array('foo'='bar');
}
}

$test = new magicExample();
$array = (array)$test;

print_r($array); // should show foo=bar, not aoeu=htns

?







-- 
Edit this bug report at https://bugs.php.net/bug.php?id=38508edit=1


Req #38508 [Com]: Addition of Magic __toArray() function

2011-05-23 Thread spark at limao dot com dot br
Edit report at http://bugs.php.net/bug.php?id=38508edit=1

 ID: 38508
 Comment by: spark at limao dot com dot br
 Reported by:doublecompile at gmail dot com
 Summary:Addition of Magic __toArray() function
 Status: Closed
 Type:   Feature/Change Request
 Package:Feature/Change Request
 PHP Version:*
 Block user comment: N
 Private report: N

 New Comment:

Because of the magic __toString I was able to write a String class with
the same methods as the Java String class.

I was trying to write an Array class to map the one in Adobe AS3, but
then I came across the lack of __toArray.

I don't want a __toArray, or even a toString

I want classes with reasonable methods and not lots of functions with no
naming rules nor parameter order standard.

I know it may sound rude but that's how I feel when I see a language
getting so far and still not following any coding convetion.


Previous Comments:

[2006-08-20 11:12:22] he...@php.net

Why not simply have a method asArray() maybe even as par of an
interface:



interface ArrayConversion

{

  function asArray();

}



See, we have __toString as it is supported in language constructs like
echo, print and other internal functions. But we decided against an
autoconversion for arrays already. So itwill never be supported in any
language construct. That said there is no needed for this and nothing
you would win against the above interface. In fact you would make it php
more complex because you'd add just one more magic feature.


[2006-08-19 00:40:17] doublecompile at gmail dot com

Description:

Doing this:



$newarray = (array)$object;



will take the properties of an object and assign them as the values of
keys in an array.



As of PHP 5.2, doing this:



$stringified = (string)$object;



will call the magic __toString() function for a user-specified
formatting of the object as a string.



It would be a great addition to call a magic __toArray() function if an
object is cast as an array, instead of converting its public members to
array elements.  (For instance, the class might not have public
members).  Classes without the function could use the default method of
mapping property names to array keys.



Just my two cents.

Reproduce code:
---
?php



class magicExample 

{

public $aoeu = htns;



function __toArray()

{

return array('foo'='bar');

}

}



$test = new magicExample();

$array = (array)$test;



print_r($array); // should show foo=bar, not aoeu=htns



?







-- 
Edit this bug report at http://bugs.php.net/bug.php?id=38508edit=1