# __autocreate() function for non-existing variables

- Thread: https://externals.io/message/4100
- Original archive: https://news-web.php.net/php.internals/4100
- Started by Vesselin Atanasov on 2003-08-20T03:10:13+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #4100 — Vesselin Atanasov — 2003-08-20T03:10:13+00:00

URL: https://externals.io/message/4100#4100

Hello.
I think that it would be useful to allow an __autocreate() function create
non-existing
variables just like __autoload is called for non-existing classes. The
function will be
called when an attempt is made to read a non-existent variable. It will
return a reference
to the new variable it creates. This could be very useful in some cases. For
example it
could be used in a php library that creates a database object. When some
function tries to
use the database object, the __autocreate function will check if a global
database object
exists. If it does not exist the function will create the database object as
global and
in either case return a reference to the object.

I am willing to implement this functionality, but I'd like to hear the
opinion of the PHP
developers first and especially of the Zend2 team because this functionality
will modify
the Zend2 engine.

vesselin

---

## #4104 — Zeev Suraski — 2003-08-20T08:01:53+00:00

URL: https://externals.io/message/4100#4104 · In reply to: #4100

I don't see need for this magic.  You can implement this using a simple 
singleton pattern, if you need it.  I don't think we want to add features 
that will condone the use of globals, a singleton class is a much better 
solution for that.

Zeev

At 06:10 20/08/2003, Vesselin Atanasov wrote:
>Hello.
>I think that it would be useful to allow an __autocreate() function create
>non-existing
>variables just like __autoload is called for non-existing classes. The
>function will be
>called when an attempt is made to read a non-existent variable. It will
>return a reference
>to the new variable it creates. This could be very useful in some cases. For
>example it
>could be used in a php library that creates a database object. When some
>function tries to
>use the database object, the __autocreate function will check if a global
>database object
>exists. If it does not exist the function will create the database object as
>global and
>in either case return a reference to the object.
>
>I am willing to implement this functionality, but I'd like to hear the
>opinion of the PHP
>developers first and especially of the Zend2 team because this functionality
>will modify
>the Zend2 engine.
>
>vesselin
