AJAX and PHP
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Server-Side Techniques with <strong>PHP</strong> <strong>and</strong> MySQL<br />
• In classes, you can implement two special methods called the constructor <strong>and</strong><br />
destructor. The constructor is called __construct(), <strong>and</strong> is executed automatically<br />
when you create new instances of a class. The constructor is useful when you have<br />
code that initializes various class members, because you can rely on it always<br />
executing as soon as a new object of the class is created.<br />
• The destructor is named __destruct(), <strong>and</strong> is called automatically when the object<br />
is destroyed. Destructors are very useful for doing housekeeping work. In most<br />
examples, we will close the database connection in the destructor, ensuring that we<br />
don't leave any database connections open, consuming unnecessary resources.<br />
• It is true that it may be a bit better for performance to create the database connection<br />
just before needing it, instead of the class constructor, <strong>and</strong> to close it right after using<br />
it, instead of the class destructor. However, we choose to use the constructor <strong>and</strong><br />
destructor because we get cleaner code where we are less likely to cause errors by<br />
forgetting to close the connection, for example.<br />
When referring to any class member, you must specify the object it is a part of. If you want<br />
to access a local class member, you must use the special $this object, that refers to the current<br />
class instance.<br />
The public interface of a class consists of its public members, which are accessible from the<br />
outside, <strong>and</strong> can be used by programs that create instances of the class. Class members can be<br />
public, private, or protected. Private members can be used only internally by the class, <strong>and</strong><br />
protected members can be used by derived classes.<br />
Separating the various layers of functionality of an application is important, because it allows you<br />
to build flexible <strong>and</strong> extensible applications that can be easily updated when necessary. In Cristian<br />
Darie <strong>and</strong> Mihai Bucica's <strong>PHP</strong> e-commerce books, you even learn how to use a templating engine<br />
called Smarty that allows you to further separate presentation logic from the HTML template, so<br />
that designers are not bothered with the programming part of the site.<br />
When preparing the design of your code, keep in mind is that the power, flexibility, <strong>and</strong><br />
scalability of the architecture is directly proportional to the time you invest in designing it<br />
<strong>and</strong> writing the foundation code. Reference to these issues is available for free download<br />
at http:// ajaxphp.packtpub.com/ajax/<br />
For this final exercise, we will build a simple but complete <strong>AJAX</strong> application called friendly, that<br />
implements many of the practices <strong>and</strong> techniques shown so far. The application will have a<br />
st<strong>and</strong>ard structure, composed of these files:<br />
• index.html is the file loaded initially by the user. It contains the JavaScript code that<br />
makes asynchronous requests to friendly.php.<br />
• friendly.css is the file containing the CSS styles to be used in the application.<br />
• friendly.js is the JavaScript file loaded together with index.html on the client<br />
side. It makes asynchronous requests to a <strong>PHP</strong> script called friendly.php to<br />
perform various functionality required to support the rich client interface.<br />
110<br />
www.it-ebooks.info