php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49957 Need magic method for every data type
Submitted: 2009-10-22 15:23 UTC Modified: 2009-10-23 15:39 UTC
From: greg at imagiclab dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.3.0 OS: unix
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: greg at imagiclab dot com
New email:
PHP Version: OS:

 

 [2009-10-22 15:23 UTC] greg at imagiclab dot com
Description:
------------
Need magic method for every data type.

Why have a incomplete magic method list. Why not a __toInt and 
__toResource.

We are trying to change a legacy system to connect to mysql on demand 
and would love a __toResource magic method.  

Also anyone who uses class based enums would like a __toInt to convert 
the enum to is int valyue and to use __toSting to convert the enum to 
its label.

Reproduce code:
---------------
---
From manual page: language.oop5.magic
---



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-23 10:24 UTC] johannes@php.net
This was discussed and leads to too many unclear situations. Like should  $a + $b call toFloat or toInt or what? What about $a < $b? Maybe a string comparison.

In the end implementing this gives more trouble than where it helps.
 [2009-10-23 15:39 UTC] greg at imagiclab dot com
Ok i see you point but other are not as complex like

__toResource would be create for connect on demand db classes


you can also only have the magic methods for float and int called if you 
are specifically cast like (int)$Object
 [2010-11-07 00:05 UTC] bob_the_builder at gmail dot com
@johannes

the problems you are mentioning are not new to OO languages. The same problem might arise in C++ for example when 2 valid implicit cast exist. This would simply raise an error. The programmer has to ability to specify casts as explicit, so they will be invoked only if it is explicity requested. Therefore he can only provide a single impicit cast to prevent such an error. The same scheme could be applied in php. If more than one implicit cast is found an E_USER error could be raised.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 08:01:27 2024 UTC