What Is a Class?


A class is the template structure that defines an object. It can contain functionsalso known as class methodsand variablesalso known as class properties or attributes.

Each class consists of a set of PHP statements that define how to perform a task or set of tasks that you want to repeat frequently. The class can contain private methods, which are only used internally to perform the class's functions, and public methods, which you can use to interface with the class.

A good class hides its inner workings and includes only the public methods that are required to provide a simple interface to its functionality. If you bundle complex blocks of programming into a class, any script that uses that class does not need to worry about exactly how a particular operation is performed. All that is required is knowledge of the class's public methods.

Because there are many freely available third-party classes for PHP, in many situations, you need not waste time implementing a feature in PHP that is already freely available.

When to Use Classes

At first, there may not appear to be any real advantage in using a class over using functions that have been modularized into an include file. OO is not necessarily a better approach to programming; rather, it is a different way of thinking. Whether you choose to develop your own classes is a matter of preference.

One of the advantages of OO programming is that it can allow your code to scale into very large projects easily. In OO programming, a class can inherit the properties of another and extend it; this means that functionality that has already been developed can be reused and adapted to fit a particular situation. This is called inheritance, and it is a key feature of OO development.

When you have completed this book, if you are interested in learning more about OO programming, take a look at Sams Teach Yourself Object-Oriented Programming in 21 Days by Anthony Sintes.

What a Class Looks Like

A class is a grouping of various functions and variablesand that is exactly how it looks when written in PHP. A class definition looks very similar to a function definition; it begins with the keyword class and an identifier, followed by the class definition, contained in a pair of curly brackets ({}).

The following is a trivial example of a class to show how a class looks. This example contains just one property, myValue, and one method, myMethod (which does nothing):

 class myClass {   var $myValue;   function myMethod() {     return 0;   } } 

If you are already familiar with OO programming and want to get a head start with OO PHP, you can refer to the online documentation at www.php.net/manual/en/language.oop5.php.



    Sams Teach Yourself PHP in 10 Minutes
    Sams Teach Yourself PHP in 10 Minutes
    ISBN: 0672327627
    EAN: 2147483647
    Year: 2005
    Pages: 151
    Authors: Chris Newman

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net