views:

529

answers:

5

I am new to php, but in other web technologies, you can share objects between page instances. For example, in java jsp pages you easily have on class that exist as static class for the whole server instance. How to do this in php?

I am not refering to sessions variables (at least I don't think so). This is more for the purpose of resource pooling (perhaps a socket to share, or database connections etc). So a whole class needs to be shared between subsequent loads, not just some primitive variables that I can store in the session.

I have also looked into doing php singleton classes but I believe that class is only shared within the same page and not across pages.

To make things even more clear, I'm looking for something that can help me share, say, a socket connected to a server for a connectSocket.php page such that all users who loads that page uses the same socket and does not open a new one.

+8  A: 

This is a bit of a difficult answer, and might not be exactly what you are looking for.

PHP is built upon a 'shared-nothing' architecture. If you require some type of state across your application, you must do this through other means.

First I would recommend looking into the core of the problem.. Do you really need it? If you assume the PHP application could die (and lose state) is it ok to lose the data?

If you must maintain the state, even after the application dies or otherwise, you should assume probably the best place to put the data is in MySQL. PHP is intended as a thin layer around your business logic, so I can highly recommend this.

If you don't care about losing the data after a restart, the problem domain you're looking for is probably caching. I would recommend looking into memcached or if you're on a single machine, apc. APC will definitely work for you with Apache on a single machine, but you will still have to code your application assuming you might lose the data.

If you're worried your underlying datastore (MySQL) is too slow, but you still need to maintain the data after a restart, you should look into a combination of these 2 systems. You can always push and pull your data from the cache, but only when it updates send it over to Mysql.

If the data is purely user or session-bound, you probably want to just looking into the sessions system.

I've personally developed a reasonably large multi-tenant application, and although its a pretty complex application, I've never needed the true state you're looking for.

Update: Sorry, I did not read your note about sharing a socket. You will need a separate daemon to handle this, perhaps if you can explain your problem further, there might be other approaches. What type of socket is this?

Evert
A: 

Most of the PHP database libraries use connection pooling already. You call, for example, pg_connect as if you were requesting a new connection, but if the connection string is the same as a connection that already exists, you will get the established connection back instead. If you only care about pooling for database access, then you can just confirm that it exists in the db library you're using.

JimG
Although your answer is correct, it's often not recommended to make use of these persistent connections. Creating new connections is often very cheap, and it means you won't have to keep all of them around for a long time.As a rule I only connect with a db when I'm doing the first query, and I get rid of it as soon as I can. Databases are difficult to scale, PHP is a lot easier.
Evert
+1  A: 

This is likely a partial answer but you can save an instance of a class into a Session variable and access it at another time.

Paulo
This unfortunately won't work with open sockets.
Evert
+5  A: 

There's a fundamental difference between web-served Java and web-served interpreted languages like PHP and Perl. In Java, your web server will have an operating environment that maintains state (ie. Tomcat). With interpreted languages, a request to your web server will generally spawn a new web server thread, which in turn loads a fresh operating environment for that thread, in this case, the PHP environment.

Therefore, in PHP, there is no concept of page instances. Every request to the web server is a fresh start. All the classes are re-loaded, so there is no concept of class sharing, nor is there a concept of resource pooling, unless it is implemented externally.

Sharing sockets between web requests therefore isn't really possible.

zombat
A: 

Hello there,

An other horroble solution may be to load the data of the object to any $_SESSION variable and then user it back into the object of the other page. In fact, this is the solution I'm going to follow in my project, until I get some better one.

Regards!

Satyendra Kumar Sharma