PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


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.




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: