_toString() is called when an object is used as string. How can I do something similar for numerical values, something like __toInt(), or __toArray(). Do such methods exist? Is there a work around? Is it a bad idea to use something like that even if there is a workaround for it?
Is there equivalent for __toString() method for other simple types in PHP?
277 Views Asked by user1848605 At
1
There are 1 best solutions below
Related Questions in PHP
- php Variable name must change in for loop
- register_shutdown_function is not getting called
- Query returning zero rows despite entries existing
- Retrieving *number* pages by page id
- Automatically closing tags in form input?
- How to resize images with PHP PARSE SDK
- how to send email from localhost using codeigniter?
- Mariadb max Error while sending QUERY packet PID
- Multiusers login redirect different page in php
- Imaginary folder when I use "DirectoryIterator" in PHP?
- CodeIgniter + XDebug: debug only working in the main controller, index() function
- PHP script timeout when I use sleep()
- posting javascript populated form to another php page
- AJAX PHP - Reload div after submit
- PHP : How can I check Array in array?
Related Questions in FUNCTION
- What is the code of the sorted function?
- I'm trying to make the "merge function" work by writing a Callback.
- Split a large query (2 days) into pieces to increase the speed in Postgres
- Recursive function in PHP function : how to prevent return value?
- Javascript function onclick
- Borland c++: Error while assigning OnChange to another function?
- Control BX Slider from outside of function
- Jquery Resize: how to check if a screen has been resized at any point
- OrientDB - SQL command to pass array value as parameter in OrientDB function
- Check if a private function exists inside an object in JavaScript
- Python - Cannot sort properly my list by alphabetical order
- refactor 'execute and log' pattern
- Javascript: Add multiple fields together and insert into Totals column on Form
- Is it possible in matlab to declare functions that will be accessible from other m files in a similar way as in c coding?
- Defining Callbacks for custom Javascript Functions
Related Questions in TYPES
- Inheritance in Java, apparent type vs actual type
- Converting 8 byte char array into long
- Derby, Java: Trouble with "CREATE_TYPE" statement
- How to tell Java that two wildcard types are the same?
- F# strange type error message
- Convert String scanner to class type
- How to access a Row Type within an Array Type in DB2 SQL PL
- Python Type Dispatching with variables, is it possible?
- Ocaml unbound type constructor with module
- Cloning a Javascript object with its type
- How to remove error of incompatible variable types in LoadLibrary() function?
- What's the difference or relationship between Type and TypeInfo?
- Scala: generic method using implicit evidence doesn't compile
- Guaranteeing data type size
- Convert String With Comma To Number Using Python Pandas
Related Questions in MAGIC-METHODS
- PHPunit call magic methods
- How to transform a method call pattern found in multiple methods into a simpler OO mechanism?
- Is there a Magic Method for type() in python?
- Magic method toString PHP
- Is __setitem__ method and square bracket assignment not the same?
- Why is a switch statement used before call_user_func_array()
- PHP Dependency Injection - magic methods injections?
- Python - 'int' object has no attribute '__getitem__'
- getting pythons __set__ working
- Python __del__ method for classes?
- magic methods - Can I use them to catch the inaccessible static property?
- How to create my own PHP magic method names?
- Built-in magic variable names/attributes
- Object roll-back, copy-on-write, versioned proxy etc in Python
- What is the __dict__.__dict__ attribute of a Python class?
Related Questions in OBJECT-TO-STRING
- Override ToString function of class
- Matplotlib objects nodes, unicode __str___
- PHP Assigning a default Function to a class
- Construct a new Object() with the toString result of another Object
- Field with random int value
- Override toString method in Java
- Exception in an Object toString() command in Android
- Convertion of user-defined object to string by toString not showing correct output
- Classes convertor
- What to return when printing out Objects using the toString method and a for each loop?
- Parse.com Getting Query and Converting It into String Java
- Failed to override toString method
- String representation of a large object model in Java?
- Using Hadoop Text Object toString() Method
- c# how to get the result string from object
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
There is no
__toArraymagic-method (just check the ones that exist here), but then, there shouldn't be, IMO.Though people have asked for a magic
toArraymethod, it doesn't look like such a method will be implemented any time soon.Considering what objects are for, and how we use them, a
toIntmethod wouldn't make much sense, and since all objects can be cast to an array, and can be iterated over, I see very little point in using__toArrayanyway.To "convert" on object to an array, you can use either one of the following methods:
This can be done with both custom objects, as
stdClassinstances alike.As far as accessing them as an array, I can't see the point. Why write a slow magic method to be able to do something like:
if you can do:
or if you can do:
Or, if you need something a tad more specific, just implement any of the
Traversableinterfaces.And for those rare cases, where you want an object to produce an array from specific properties in a very particular way, just write a method for that and call that method explicitly.
Ok, you have to call these methods yourself, explicitly, but that's not that hard, really... is it?