php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #76295 Add a __toHeredoc magic method
Submitted: 2018-05-02 12:33 UTC Modified: 2018-05-02 12:45 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: email at davekok dot nl Assigned:
Status: Wont fix Package: *General Issues
PHP Version: Next Major Version OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2018-05-02 12:33 UTC] email at davekok dot nl
Description:
------------
Feature request.

At moment PHP has the very useful __toString() magic method. However at times I find myself needing to convert an object to different string representations based on the output language used, like HTML, CSS, Json, CSV. When using heredoc it would be useful if the identifier could be used to distinguish between different types of languages similar to how markdown works.

Would it be possible to add a __toHeredoc(string $identifier) magic method that a programmer could implement? With a fallback to using __toString() magic method if it is missing?


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-05-02 12:45 UTC] requinix@php.net
-Status: Open +Status: Wont fix
 [2018-05-02 12:45 UTC] requinix@php.net
There is more to this than just adding another magic method. It has some serious implications about what heredocs can be and how they should work. And I can think of some complexities regarding when it should be used or not that would need to be addressed.

But really, this is something that can be done easily with a regular userland method.

echo <<<FOO
<p>This is a {$object->render("html")} object.</p>
FOO;
echo <<<BAR
This is a {$object->render("text")} object.
BAR;

If you feel strongly about PHP needing dedicated support for this then check out the RFC process.
https://wiki.php.net/rfc/howto
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 10:01:26 2024 UTC