Beginning PHP
     Session #6
   January 5, 2011
     Josh Butts
Agenda


• Freebies from Zend
• OOP
What are Objects

• Programming constructs
• Represent a real-life entity
• Store data (state)
• Implement behaviors
Object Oriented
      Programming
• ...programming with objects
• Use objects to model real world concepts
  in your code
• Objects encapsulate pieces of logic or data
• Objects are easy to re-use
Terms

• Class - the definition of an object; what
  does it represent, what can it do
• Instance - an actual object variable in
  memory
Classes & Instances

• A class is defined in code as you write your
  program
• An instance happens when you program
  actually runs
Example
   User Class
                             Instance of User
  $username
  $password
                              jimbojsb
    $email
                              password
  $firstname
                         josh@joshbutts.com
  $lastname
                                 Josh
                                Butts
  authenticate()
createPassword()
    getPosts()
Classes & Instances

• A class is defined in code as you write your
  program
• An instance happens when you program
  actually runs
“Static”
• Static describes a class or part of a class
  that can be used without actually creating
  an instance and filling it with data
• Pieces of data that are the same for every
  instance of a class
• Logic that operates on generic data rather
  than modifying a specific instance’s state
Building Objects
• Properties (variables)

• Methods (functions)

• Methods & Properties can both be static or
  not
Constructors
• A special method (1 per object) that gets
  called when you get a new instance
• Does any utility or setup work needed
  before the instance is ready to be used
• Optional
• Usually take one or more parameters
PHP Objects
• Preferably you define one class per file
• Objects are always passed by reference
  instead of by value
• :: operator is used for static properties and
  methods
• -> operator is used for instance properties
  and methods
PHP Objects


• self - refers to the current object in a static
  manner
• $this - refers to the current instance
Special Methods

• __construct()
• __destruct()
• __toString()
• many others, for a later discussion

GeekAustin PHP Class - Session 6

  • 1.
    Beginning PHP Session #6 January 5, 2011 Josh Butts
  • 2.
  • 3.
    What are Objects •Programming constructs • Represent a real-life entity • Store data (state) • Implement behaviors
  • 4.
    Object Oriented Programming • ...programming with objects • Use objects to model real world concepts in your code • Objects encapsulate pieces of logic or data • Objects are easy to re-use
  • 5.
    Terms • Class -the definition of an object; what does it represent, what can it do • Instance - an actual object variable in memory
  • 6.
    Classes & Instances •A class is defined in code as you write your program • An instance happens when you program actually runs
  • 7.
    Example User Class Instance of User $username $password jimbojsb $email password $firstname josh@joshbutts.com $lastname Josh Butts authenticate() createPassword() getPosts()
  • 8.
    Classes & Instances •A class is defined in code as you write your program • An instance happens when you program actually runs
  • 9.
    “Static” • Static describesa class or part of a class that can be used without actually creating an instance and filling it with data • Pieces of data that are the same for every instance of a class • Logic that operates on generic data rather than modifying a specific instance’s state
  • 10.
    Building Objects • Properties(variables) • Methods (functions) • Methods & Properties can both be static or not
  • 11.
    Constructors • A specialmethod (1 per object) that gets called when you get a new instance • Does any utility or setup work needed before the instance is ready to be used • Optional • Usually take one or more parameters
  • 12.
    PHP Objects • Preferablyyou define one class per file • Objects are always passed by reference instead of by value • :: operator is used for static properties and methods • -> operator is used for instance properties and methods
  • 13.
    PHP Objects • self- refers to the current object in a static manner • $this - refers to the current instance
  • 14.
    Special Methods • __construct() •__destruct() • __toString() • many others, for a later discussion