Hi everyone,

I have noticed that it is not possible to do something like that:

// foo.php
<?php
return ['foo' => 'bar'];

// bar.php
<?php
var_dump((require 'foo.php')['foo']);

Array access on a require does not work: we have a parse error. Is it just a parser issue? Because a file can return an array without any problem. For now, one possible solution is to write a function that requires a file and returns the result, such as:

// bar.php
<?php
function my_require ( $file ) { return require $file; }
var_dump(my_require('foo'.php')['foo']);

It would be nice if require (and require_once, include and include_once) instructions would behave like functions when returning values.
Thoughts?

In the same way, it is not possible for now to do something like that:
function f ( ) {
return function ( $msg ) {

return $msg . '!';
}
}
f()('foo');
We have a parse error again. Is it a parser issue?
It could be interesting to have this feature in PHP because file can return closure! It would be great to extend this syntax on require and others instructions, such as:

<?php
(require 'baz.php')('a message'); // chaining methods calls

Finally, what about exporting only some symbols? It could be a middle step between declaring classes as private, protected and public and all public. We can imagine somethings like:

// foo.php
<?php
class IAmPrivate { }
class IAmPublic { }

export [IAmPublic]; // or export [IAmPublic::class], please see current discussion about Classname::class proposition.

Here, the “export” keyword will act like “return” but it will specify that all declaring symbols inside the file will not be exposed outside the file except specified ones. It could add a “module aspect” to PHP, and it does not break the compatibility.

Thoughts?

Best regards.

--
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://lifc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/



--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to