Jump to content

So... I've decided to teach myself Object-Oriented PHP.


DZComposer

Recommended Posts

I figure if I am going to write this RP System (Yes, I have tentatively named it "RP System." How original :roll: ) program for the RPs, I may as well do it up to current PHP coding standards, which means object-oriented.

Only thing is, I have never written anything in an OO style before. O_o

Until today.

Today, I wrote my very first Object-Oriented program:

<?php
// OO Test Scripts
//Class 1: Counter

//define counter class

class Counter {

protected $MyCounter;

function ZeroCounter(){
	$this->MyCounter = 0;
}
function InitializeCounter($value){
	$this->MyCounter = $value;
}

function IncrementCounter(){
	$this->MyCounter++;
}
function GetCounterValue(){
	return $this->MyCounter;
}
}

//define reverse counter subclass
class ReverseCounter extends Counter {

function DecrementCounter(){
	$this->MyCounter--;
}

}

//Instantiate classes and declare variables
$cinst = $cntr = new Counter();
$rinst = $rcntr = new ReverseCounter();
$initval = 12;
$loopctrl = 10;

//Initialize Counters
$cinit = $cinst -> InitializeCounter($initval);
$rinit = $rinst -> InitializeCounter($initval);

$c_val = $cinst -> GetCounterValue();
$r_val = $rinst -> GetCounterValue();

echo "Standard Counter initialized at $c_val. <br>";
echo "Reverse Counter initialized at $r_val. <br><br>";
echo "Processing... <br><br>";

while ($loopctrl > 0){
	$cinst -> IncrementCounter();
	$rinst -> DecrementCounter();
	$c_val = $cinst -> GetCounterValue();
	$r_val = $rinst -> GetCounterValue();
	echo "Standard Counter Value: $c_val. <br>";
	echo "Reverse Counter Value: $r_val. <br><br>";
	echo "Processing... <br><br>";
	$loopctrl--;
}
?>

It's pretty useless, admittedly. All it does is use a class and a subclass to create two counters. One counts up, and one counts down. It does this ten times via a while loop.

Here is its output:

Standard Counter initialized at 12.

Reverse Counter initialized at 12.

Processing...

Standard Counter Value: 13.

Reverse Counter Value: 11.

Processing...

Standard Counter Value: 14.

Reverse Counter Value: 10.

Processing...

Standard Counter Value: 15.

Reverse Counter Value: 9.

Processing...

Standard Counter Value: 16.

Reverse Counter Value: 8.

Processing...

Standard Counter Value: 17.

Reverse Counter Value: 7.

Processing...

Standard Counter Value: 18.

Reverse Counter Value: 6.

Processing...

Standard Counter Value: 19.

Reverse Counter Value: 5.

Processing...

Standard Counter Value: 20.

Reverse Counter Value: 4.

Processing...

Standard Counter Value: 21.

Reverse Counter Value: 3.

Processing...

Standard Counter Value: 22.

Reverse Counter Value: 2.

Processing...

Yes, I know I can use an if statement to kill that last "Processing...", but that wasn't the point of this exercise.

The nice thing about Object-Oriented Programming (OOP) is that I can create as many counters as I want without writing the individual code for them. While I could have done this procedurally with regular functions, this allows me to bundle my functions together in nice little packages that can be handled independently of each other (IE the main advantage of OO programming). I literally could use the same functions to track 50 different counters independently. It is unbelievable how bad-ass this is, and I wish I would have tried this earlier!

Many props to this: http://www.aonaware.com/OOP1.htm

That tutorial is the best explanation of OOP that I have seen. Most are language-specific and are a bit confusing. I found this one straignt-forward. Admittedly, I got the algorithm for this little program from that site, but hey, why invent a problem when there already is one?

If any of you have experience with OO PHP, feel free to shoot me any tips. I am in 100% learning mode with this right now.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...