php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #76887 New Useful Magic Methods
Submitted: 2018-09-15 21:26 UTC Modified: 2018-09-15 21:34 UTC
From: klaussantana at gmail dot com Assigned:
Status: Duplicate Package: Class/Object related
PHP Version: Irrelevant OS: Any
Private report: No CVE-ID: None
 [2018-09-15 21:26 UTC] klaussantana at gmail dot com
Description:
------------
I'm sugesting some new magic methods for objects that'll add some useful behaviours to objects in new versions of PHP.

1. __toNumber()
Do some calculations on the object and return a number automatically when an object is casted as a number, be it integer or float. Useful to AI scripts for calculating the numerical weight of an object and many other stuff. Can be achieved by using "(float) ( (string) $Object )", but this costs a lot of characters in every expression and misuses the "__toString" method.

2. __toArray()
Must create and return a raw array with keys being the names of the properties of the working object and with their respective values being references to their counterparts in the working object. Pretty much do the same things achieved by "ArrayIterator::getArrayCopy()", but slightly different. Automatically called when the object is casted as an array (eg.: "(array) $Object").

This method scope could detect it's parent's in a way that it can return the properties acordingly with the scope that called the method. For example, if in an outer scope it will only return the public properties, if from an inner scope it will return protected or private properties as well.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-09-15 21:34 UTC] requinix@php.net
-Status: Open +Status: Duplicate -Package: Reflection related +Package: Class/Object related
 [2018-09-15 21:34 UTC] requinix@php.net
This is something that needs to be discussed on the internals list.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 05:01:33 2025 UTC