Introduction to ObjectOriented Programming with PHP 5 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Introduction to ObjectOriented Programming with PHP 5

Description:

... function calls to their object won't accidentially change the variable settings of the class. ... var $attribute1; var $attribute2; //operations. function ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 11
Provided by: paws3
Category:

less

Transcript and Presenter's Notes

Title: Introduction to ObjectOriented Programming with PHP 5


1
Chapter 4
  • Introduction to Object-Oriented Programming with
    PHP 5

2
Why Create Objects?
  • Helps manage complexity within projects.
  • Increases code reusability thereby reducing
    maintenance cost.

3
Classes and Objects
  • Object can be almost any item or concept such as
    a desk or a customer
  • Conceptual objects also exist but usually only in
    software
  • Text input area or a file.
  • Classes represents a set of objects that might
    vary from individual to individual but have a
    certain amount of attributes and operations in
    common
  • Cars and bikes.

4
Attributes and Operations
  • OO software is designed and built as a set of
    self-contained objects with both attributes and
    operations
  • Attributes are also called member variables or
    properties.
  • Operations are also called methods.

5
Encapsulation
  • Also known as data hiding.
  • Data within the object is available only via the
    objects operations (the interface of the object.
  • Encapsulation give the programmer a way to ensure
    that function calls to their object wont
    accidentially change the variable settings of the
    class.

6
Structure of a Class
  • class classname
  • // member variables
  • var attribute1
  • var attribute2
  • //operations
  • function operation1()
  • function operation2(param1, param2)

7
UnitCounter Example
  • lt?php
  • class UnitCounter
  • // member variables
  • var units 0
  • var weightPerUnit 1.0
  • // operations
  • // Add n to the total number of units,
    default1
  • function add(n 1)
  • this-gtunits this-gtunits n
  • function totalWeight()
  • return this-gtunits this-gtweightPerUnit
  • ?gt

8
Constructors
  • A constructor is a special type of operation.
  • A constructor is automatically called when an
    object is created.
  • Its function is usually to perform useful
    initialization tasks such as setting attributes
    to sensible starting values or defining ranges
    for attributes.
  • Although they are called automatically, you can
    call them using the __contruct(param). This is
    new in PHP5. PHP4 used the convention of the
    constructor having the same name as the object.

9
Instantiating a Class
  • After you have created a class, you will want to
    use it. This is called instantiation or creating
    an instance of the class.
  • Try it with the following code example

10
Instantiating a Class, Example
  • lt?php
  • class classname
  • function __construct(param)
  • echo 'Constructor called with parameter ' .
    param . 'ltbr /gt'
  • a new classname(First)
  • b new classname(Second)
  • c new classname()
  • ?gt
Write a Comment
User Comments (0)
About PowerShow.com