PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters

Introduction to PHP Objects, Part 1
Pages: 1, 2

Member Functions and the $this Reference Variable

Please note that, at least by convention, developers rarely access member variables directly. Rather, it is the job of the class to provide functions that modify any member variables that the developer may need to modify. With that said, let's create a new class, counter, that creates two member variables, $step and $count, and member functions to work with them.



<?php

class counter {
     var $step;
     var $count; 

     function getcount() {
          return $this->count;
     }
 
     function getstep() {
          return $this->step;
     }

     function changestep($newval) {
          if(is_integer($newval))
          $this->step = $newval;
     }

     function step() {
          $this->count += $this->step;
     }

     function reset() {
          $this->count = 0;
          $this->step = 1;
     }
}
?>

Now, let's take a look at what is really involved in the example above. First, we declare the two member variables $step and $count, which you have already seen in prior examples. Next, we begin the creation of five functions: getcount(), getstep(), changestep(), reset(), and step(). The code contained within these functions is very simple, but there is one thing that probably seems a bit alien -- the use of the seemingly-undeclared variable $this. The variable $this is available from anywhere within any PHP class and is created by PHP when an instance of the class is created. Basically, the $this variable is a reference to the created instance itself, just as $myobject was a reference to the foo_object class. As you can see through the use of the $this variable, you can access variables within an instance of a class, from within the instance itself.

Now that we are dealing with functions, let's take the example class we have created above and put it to a semi-useful purpose.

Also in PHP Foundations:

Using MySQL from PHP, Part 2

Using MySQL from PHP

MySQL Crash Course, Part 3

MySQL Crash Course, Part 2

MySQL Crash Course

<?php

$ticker = new counter();

$ticker->reset();

            $ticker->changestep(2); 

            while($ticker->getcount() <= 20) {

echo "The value of the ticker is " .

                        $ticker->getcount()."<BR>";

$ticker->step();

}

?>

Can you follow the code above, looking at the class definition? First, we create an instance of the class and store a reference to it in the variable $ticker. Next, we call the member function reset(), which resets the counter to its original state and sets the step-count to 2 (meaning that it will count by twos). Then, we loop until the ticker has reached the value of 20. The result will be the following output:

The value of the ticker is 0

The value of the ticker is 2

The value of the ticker is 4

...

The value of the ticker is 20

More to Come

Now that I've covered the very basics of object-oriented programming in PHP, I suggest you work with them a bit and make sure you understand them completely before moving on! In my next column, I'll be discussing some more advanced topics, including object inheritance, special member functions, and more!

John Coggeshall is a a PHP consultant and author who started losing sleep over PHP around five years ago.


Read more PHP Foundations columns.

Discuss this article in the O'Reilly Network PHP Forum.

Return to the PHP DevCenter.




Tagged Articles

Post to del.icio.us

This article has been tagged:

php

Articles that share the tag php:

Understanding MVC in PHP (477 tags)

The PHP Scalability Myth (123 tags)

The Dynamic Duo of PEAR::DB and Smarty (53 tags)

PHP Form Handling (43 tags)

Very Dynamic Web Interfaces (39 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com