I’ve implemented a REST-based API (using Tonic, FWIW) so I have a central dispatch.php file that handles sending URI requests off to the appropriate classes. At the back end I have MySQL through PDO.
To implement my higher-level functionality I need to call about three or four REST APIs in a row, so I worry that all the construction and destruction of the PDO object and the connecting and disconnecting from the back-end database adds unnecessary overhead. I could store the PDO in the $SESSION array, I suppose, but that doesn’t feel like the right way to go about things. What is the recommended way to do this?
This question is really orthogonal to REST, and is a problem in just about every PHP application.
In any standard PHP application, the everything is created on the request, and destroyed at the end. This is the “share-nothing architecture”.
That said, there is a project called appserver-in-php, which attempts to address this, by creating a server in PHP. If you use this, you can create you PDO object once, and have it available until you shut down the server, or a snake bites the ethernet cables.