Class in PHP

3
 1 PHP: A Beginner's . Guide Creating Classes PHP's support for OOP was limited; however, PHP 5.0 introduced a new object model that allowed programmers significantl greater fle!ibilit and ease of use when wor"ing with classes and objects. Introducing Classes and Objects #lass is a self$contained, independent collection of variables and functions, which wor" together to perform one or more specific tas"s. %ariables within a class are called prop erties; functions are called methods. #lasses serve as templates for objects, which are specific instances of a class. &ver object has properties and methods corresponding to those of its parent c lass. &ver object instance is completel inde pendent, with its own properties and methods, and can thus be manipulated independentl of other objects of the same class. o put this in more concrete terms, consider an e!ample( an )utomobile class that contain properties for color and ma"e, and methods for acceleration, bra "ing, and turning. *t's possible to derive two independent objects from this )utomobile class, one representing a +ord and the other a Honda. &ach of these objects would have methods for acceleration, bra"ing, and turning, as well as specific values for color and ma"e. &ach object instance could also be manipulated independentl( for e!ample, ou could change the Honda's color without affecting the +ord, or call the +ord's acceleration method without an impact on the Honda. Defining and Using Classes *n PHP, classes are defined much li"e functions( a class definition begins with the class "eword, which is followed  b the class name and a pair of curl braces. #lass definition must be enclosed within these braces; in most cases, this definition consists of propert variable- definitions followed b method function- definitions. o see what a class definition loo"s li"e, review the following listing( it contains a definition for an )utomobile class, with two properties named color and ma"e and methods named accelerate -,  br a" e - , and turn -( /php class )utomobile 11 class definition {  public color; public ma"e; 11 properties  public function accelerate- { echo ')ccelerating...'; 11 methods }  public function bra"e- { echo '2lowing down...'; }  public function turn- { echo 'urning...';

description

Object Oriented Programming in PHP

Transcript of Class in PHP

PHP : a Beginner's Guide {McGraw Hill Professional}

Chapter 5:Using Functions and Classes .1.3.7.2PHP: A Beginner's . Guide

Creating ClassesPHP's support for OOP was limited; however, PHP 5.0 introduced a new object model that allowed programmers significantly greater flexibility and ease of use when working with classes and objects.

Introducing Classes and ObjectsClass is a self-contained, independent collection of variables and functions, which work together to perform one or more specific tasks. Variables within a class are called properties; functions are called methods.

Classes serve as templates for objects, which are specific instances of a class. Every object has properties and methods corresponding to those of its parent class. Every object instance is completely independent, with its own properties and methods, and can thus be manipulated independently of other objects of the same class.

To put this in more concrete terms, consider an example: an Automobile class that contain properties for color and make, and methods for acceleration, braking, and turning. It's possible to derive two independent objects from this Automobile class, one representing a Ford and the other a Honda. Each of these objects would have methods for acceleration, braking, and turning, as well as specific values for color and make. Each object instance could also be manipulated independently: for example, you could change the Honda's color without affecting the Ford, or call the Ford's acceleration method without any impact on the Honda.

Defining and Using ClassesIn PHP, classes are defined much like functions: a class definition begins with the class keyword, which is followed by the class name and a pair of curly braces. Class definition must be enclosed within these braces; in most cases, this definition consists of property (variable) definitions followed by method (function) definitions.

To see what a class definition looks like, review the following listing: it contains a definition for an Automobile class, with two properties named $color and $make and methods named accelerate (), brake (), and turn ():

To access or change a class method or property from within the class itself, it's necessary to prefix the corresponding method or property name with $this, which refers to "this" class. To see how this works, consider this revision of the preceding example, which sets a class property named $speed and then modifies this property from within the accelerate () and brake () functions: