3

The code below is how I used to cache objects in Java.

class Account{
    private static ArrayList<Account> accounts = new ArrayList<Account>(); //Array that holds Account objects
    private String username; //Unique Name or Username

    public Account(String username) { // constructor
        this.username = username;
        Account.accounts.add(this); //Add object to accounts Array
    }

    public String getUsername() { 
        return this.username; // Return username
    }

    public Account getAccount(String username) { //Get object "Account" with username
        for (Account acc: Account.accounts) { //Foreach loop that loop over all accounts from the Array
            if (acc.getUsername() == username) return acc; // Return Account object if given username == object's username
        }
        return null;
    }
}

I commented it so it will make sense if you don't understand Java but Java OOP is similar to PHP OOP.

From the Java code above I can hold all objects on an Array, So it doesn't query the Database all the time.

1) I'm wondering if I can do something similar with PHP to speed up code and cache classes. If this is possible can you please show me an example. If not what would be the best way to achieve this?

2) What are some good practices to use when object oriented programming to keep memory usage low?

Thanks in advance

5
  • 1
    You do realise that PHP scripts only "live" within the lifetime of a request, right? If you want to retain a cache you would need to use an external mechanism such as memcache or static files. Commented Apr 16, 2014 at 5:07
  • Oh I thought I make PHP classes static. I apologize If I'm talking nonsense here, I'm fairly new to PHP. Commented Apr 16, 2014 at 5:12
  • I think it would be nice if there is an explanation to this question as it's a good source for PHP newcomers. Commented Apr 16, 2014 at 5:21
  • Yes, An explanation would be nice. Now I know it wouldn't work as with Java it would. What could I do instead? or should I just create a new account class and query the database each time the page get reload or when the user go to an another page that require account class? Commented Apr 16, 2014 at 5:32
  • You could store the account inside a session, assuming this is a service that requires some kind of login system. Commented Apr 16, 2014 at 5:43

1 Answer 1

1

The biggest difference between a Java app and a PHP app is that Java is typically a constantly running program handling several incoming connections at once, while PHP instances are started and torn down by the hosting web server for every single individual request. That means any class you load or object you instantiate or variable you assign only has a lifetime of a number of milliseconds (in a decently fast application). Static class properties work the same as in Java, however, the entire application is torn down after a few milliseconds, so this doesn't work as a long-term cache. And more importantly, each individual HTTP request is its own independent thread, so assigning something in one thread doesn't make it visible in any other simultaneous or subsequent threads.

To cache something cross-request, you need an external data store. For this you have many options, depending on what suites you best:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.