AJAX and PHP
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Chapter 3<br />
On the server side, all the interesting things happen in friendly.class.php, which is called from<br />
friendly.php to do the interesting part of the work. In friendly.class.php you can find the<br />
Friendly class, which has the following four members:<br />
• $mMysqli: A private field that stores an open database connection during the life of<br />
the object.<br />
• __construct(): The class constructor initializes $mMysqli by opening a database<br />
connection. Because the constructor is executed automatically when an instance of<br />
the class is created, you can safely assume to have the connection available in all<br />
methods of the class.<br />
• __destruct(): The class destructor closes the database connection. The destructor is<br />
executed automatically when the class instance is destroyed.<br />
• getR<strong>and</strong>omNumber(): This is a private helper method that returns a r<strong>and</strong>om number.<br />
Private methods can't be called from programs that create instances of the class, <strong>and</strong><br />
are meant to provide internal functionality only. The code in getR<strong>and</strong>omNumber is<br />
familiar from the previous exercises, as it calls the external r<strong>and</strong>om.org server to<br />
retrieve new r<strong>and</strong>om numbers. The usleep <strong>PHP</strong> function is used to artificially add a<br />
quarter of a second delay, so that you can admire the "Receiving new message from<br />
server…" message on the client for a little longer.<br />
• getNews(): This is a public method that an external program can access to get a<br />
new "news" message. The method gets two r<strong>and</strong>om user names from the database,<br />
uses the getR<strong>and</strong>omNumber method to retrieve a r<strong>and</strong>om number, <strong>and</strong> composes a<br />
message such as "User x works with user y at project #z". (Yes that's not very<br />
imaginative but we couldn't think of anything more interesting—sorry!) Note the<br />
$this special object that is used to access $mMysqli <strong>and</strong> getR<strong>and</strong>omNumber().<br />
Class members can only be accessed using an instance of the class <strong>and</strong> in <strong>PHP</strong><br />
$this refers to the current class instance.<br />
Summary<br />
Hopefully, you have enjoyed the little examples of this chapter, because many more will follow!<br />
This chapter walked you through the technologies that live at the server side of a typical <strong>AJAX</strong><br />
application. We have done a few exercises that involved simple server functionality, <strong>and</strong> <strong>PHP</strong> did<br />
a wonderful job at delivering that functionality. You have also learned the basics of working with<br />
databases, <strong>and</strong> simple database operations with the first table created in this book.<br />
In the following chapters, you'll meet even more interesting examples that use more advanced<br />
code to implement their functionality. In Chapter 4, you'll build an <strong>AJAX</strong>-enabled form validation<br />
page, which is safe to work even if the client doesn't support JavaScript <strong>and</strong> <strong>AJAX</strong>.<br />
119<br />
www.it-ebooks.info