+ Reply to Thread
Results 1 to 6 of 6

Thread: PHP Objects

  1. #1
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    PHP Objects

    Object Oriented Programming

    OOP is a method of programming where objects are designed and communicate with each other to solve a problem.

    The three major ideas of object-oriented programming (OOP) are:

    1. Inheritance
    2. Polymorphism
    3. Encapsulation

    These subjects are for later tutorials.

    Objects are designed to represent real world objects. Some objects in the real world include: books, paper, and a shoe. Anything that has a unique identity can be considered an object.

    Object Oriented Languages

    There are several object-oriented programming languages. A few of these languages include: PHP, C++, and Java. Of all these languages Java is a full object-oriented language, where as C++ and PHP are not. They include object features but are not a fully object-oriented language.

    The focus on this tutorial is object-oriented programming in the PHP language.

    Defining an object

    You use the class keyword to define a class. A class is a template that you can use to create several instances of an object.

    Example:

    Code:
    class player {


    A class is a method of packaging data and methods in one coherent package. The methods act on the data. This class above lets you construct several objects of type player.

    Constructors

    A object is created by using a constructor. In Java, a constructor is defined by writing a function that named after the type of object.

    Example the constructor a player object would look like:

    Code:
    player() {


    Constructors in PHP5 can be written in this method.

    Example:

    Code:
    function test($a,$b) {
        
    $this->$a;
        
    $this->$b;

    This is more PHP4 style constructor. There is a newer way of writing constructors.

    Code:
    function __construct($a,$b) {
        
    $this->$a;
        
    $this->$b;

    You call the constructor by using the new operator. This creates a new object.

    Example:

    Code:
    $test = new test(5,3); 
    Now $test is a reference to a test object. This is important to note that variables holding objects are actually a reference to the objects location in memory.

    The $this Variable

    this is a special variable that refers to the current object. Which is why in my constructor I used $this->a = $a. This means that in the current object the $a variable should be set to the local $a variable.

    An example class:

    Code:
    class test {
        public 
    $a;
        public 
    $b;
        
        function 
    __construct($a,$b) {
            
    $this->$a;
            
    $this->$b;
        }

    If you left off $this->a and just put $a = $a. The local variable $a would have its value changed. This is a common thing to get confused about if you come from java where using this is optional. In PHP you must include the $this operator.

    Instance variables

    $a and $b defined at the beginning of test are instance variables of the class. Any object created of type test will have it's own $a and $b variable.

    Example:

    Code:
    $test1 = new test(5,3);
    $test2 = new test(4,2);
    echo 
    "a = {$test1->a}, b = {$test1->b}<br />";
    echo 
    "a = {$test2->a}, b = {$test2->b}<br />"
    When you run this the output would be:

    a = 5, b = 3
    a = 4, b = 2
    This example illustrates that each object is unique from each other, and that they have their own variables.

    Instance Methods

    Functions in a class are referred to as methods. A method is defined to act on data in the object. A class method is written in the same way as writing a function.

    Code:
    function getA() {
        return 
    $this->a;

    This method called a getter just returns the value of $a. Every object created from the class would have it's own getA method. The class again with this method:

    Code:
    class test {
        public 
    $a;
        public 
    $b;
        
        function 
    __construct($a,$b) {
            
    $this->$a;
            
    $this->$b;
        }
        
        function 
    getA() {
            return 
    $this->a;
        }

    Example of calling the function:

    Code:
    $test = new test(5,3);
    echo 
    $test->getA(); 
    This would output just "5". The proper use of getters and setter with visibility modifiers can make your data more secure by only allowing parts of program access to the data it absolutely requires. More on access modifiers later.

    Destructors

    When an object is no longer used it's destructor is called. This is a special function that usually is used to handle cleanup the object and freeing up memory.

    You can write a destructor like this:

    Code:
    function __destructor() {
        echo 
    "Bye!";

    When the script is done executing all destructors are called to clean up memory.

    Different Control Modifiers

    Control modifiers allow you to control what parts of your program can access other parts of the program. This can make your program easier to maintain and provide for more security. A class that maintains information about how many visits a page has received should have absolutely no access to the customer credit card info. With proper use of access modifiers the object only cans access to variables and methods that is absolutely requires access to.

    The different access modifiers are:

    public
    When you use the public modifier the variable or method can be accessed by any object or function. This is the default access modifier.

    private
    When you use the private modifier the variable or method can only be accessed by the object that it is a part of. Most instance variables should be declared private and use the appropriate setters and getter methods.

    final
    This variable or function is declared final. This means that you cannot override this variable in an inherited class.

    Abstract
    This means that you cannot initialize this class. You have to inherit from this class. The "abstract" keyword indicates an abstract class.

    protected
    protected means that the variable or function can only be used by the object or classes that are inheriting it.

    Setters and Getters

    A setter is a method that updates a variable. This is more secure than allowing direct access to a variable. Example: with a setter you can make sure that the user has enough money in their account for a withdraw.

    Example:

    Code:
    function setBalance($amount) {
        if (
    $amount $this->amount) {
            echo 
    "You don't have enough money to make this withdraw.";
            return 
    false;
        }
        
    $this->amount -= $amount;
        return 
    true;

    This setter function named with the word "set" only updates the balance in the object if the correct amount exists to make the withdrawl. In the class definition "$amount" would be declared private.

    A getter method simply returns one of the objects variables.

    Example:

    Code:
    public function getName() {
        return 
    $this->name;

    This method if placed in a class would return the value of the objects $name variable.

    Inheritance and Constructors

    If you are inheriting a class and you would like to call the superclass' constructor and slightly modify it you can use the parent::__construct() function. This special function calls the superclass' constructor. You would use this is an overridden constructor.

    For example:

    Code:
    class person extends DNA {
        public 
    $name "";
        public 
    $age;
        
        function 
    __construct($name,$age) {
            
    // call the DNA constructor to create this persons DNA structure
            
    parent::__construct($name);
            
    $this->age $age;
        }

    The extends keyword allows you to create a new class based on a different class. This is known as "inheritance". The new class gains access to all public, and protected variables of the parent.

    You can delete objects by using the unset method.

    Copying Objects

    The one great thing about the object model in PHP is it allows easy copying of objects. Say you are implementing a recursive walkabout through a maze and you need several copies of an object you can use the clone keyword to pass a copy.

    Example:

    Code:
    function walkabout($maze) {
        
    // at the beginning of the recursive walkabout we need a new copy of the maze object
        
    $copy = clone $maze

    Here the $copy variable is a copy of the $maze object.

    This is an introduction to object based programming. Using objects correctly can make your program easier to understand and a lot easier to maintain. Not to mention securing your data in a more easier way.

    Enjoy,
    James

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,697
    Blog Entries
    57

    Re: PHP Objects

    Keep up the quest for rep!
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: PHP Objects

    Very nice tutorial! For an excellent book resource on PHP OOP, I recommend PHP Objects, Patterns and Practice. Are you going to be learning design patterns next?

    +rep

  4. #4
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    Re: PHP Objects

    Yeah might be a good read. Gonna go look for that book in a bit.

    Thanks guys!

  5. #5
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25

    Re: PHP Objects

    You could be a little more clear on your explanation of "Inheritance and Constructors," but everything else is very thorough. Nice work!

  6. #6
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    Re: PHP Objects

    Your right my inheritance section wasn't that thorough but it is a good idea for a future topic to write about. I'll write more about constructors also.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. PHP 5 and OOP
    By Jordan in forum PHP Tutorials
    Replies: 11
    Last Post: 09-22-2008, 01:58 AM
  2. PHP 4 end of life announcement
    By Jordan in forum News
    Replies: 4
    Last Post: 08-30-2007, 09:55 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts