
<?
class dummy {
var $variable;
var $variable2 = 1;
var $variable3 = 2;
function sum($one, $two) {
$val = $one + $two;
return $val;
}
?>
Hopefully that wasn't too scary. As you might have guessed, the line "class dummy {"
initializes the class. "dummy" is now inherited as the name of the class. The purpose
of this will be shown later. Declaring global variables in classes however are a tad
different then regular PHP. They must be preceded by "var". (For all you C coders,
this is identical to "int variable;") Next, we see a function in it's most purest
form. "function sum($one, $two) {" intializes the function by the name of "sum" with
the required arguments $one and $two. The function now adds the value of $one and
$two, and returns the value quite obviously through "return $val;". Got it? Time to
add more :-)
<?
class dummy {
var $variable;
var $variable2 = 1;
var $variable3 = 2;
function sum($one, $two) {
$val = $one + $two;
return $val;
}
function sum2() {
$val = $this->sum($this->variable2, $this->variable3);
return $val;
}
}
?>
WHOA! DON'T GET SCARED! What we just did right there is one of the most important
aspects of object-oriented programming called "inheritance." Let's look at this line
for line. All our variables are declared through the "var" procedure, that's nothing
new. Nor is the first function sum(). However, sum2() is. First off, you might notice
there are no arguments for this variable. That's because this variable is a void, much
like numerous internal PHP functions like time() or others I can't think of. This line
displays the inheritance. The "$this" variable is a reserved variable in
object-oriented PHP programming (translation - don't use it in your classes!) that
actually does the inheritance. "$this->sum()" is basically interpreted as, "in this
class, perform the sum() function." Like "$this->sum()", "$this->variable2" is
interpreted as "in this class, use the value of 'variable2'." Likewise with
$this->variable3. Note: all calls to native class functions or variables are done in
this manner. Starting with "$this->" and followed by the name of the function or
variable you wish to use. I can't stress the importance of that enough. Now I want to
show you something imperative to object-oriented programming, using all this outside
of your classes. This is how it's done if your class is contained in an external file:
<?
include("/path/to/the/file/with/your/class/in/it");
$yourclass = new yourclassname;
echo $yourclass->somevariable;
?>
If the class is internal, simple exclude the include() line. Now this is more
inheritance. "$yourclass" inherits all of the class because of the usage of the "new
yourclassname". Obviously, you would replace "yourclassname" with the name of your
class in your script. "$yourclass" is now used the same way as "$this" was inside of
your class, as shown from the "$yourclass->somevariable" reference. Got all that?
Good.
<?
class dummy {
var $variable;
var $variable2 = 1;
var $variable3 = 2;
function sum($one, $two) {
$val = $one + $two;
return $val;
}
function sum2() {
$val = $this->sum($this->variable2, $this->variable3);
return $val;
}
}
class dummy2 extends dummy {
function sum2_clone() {
$val = $this->sum($this->variable2, $this->variable3);
return $val;
}
}
?>
Ok now this is just fun. The "sum2_clone()" function will return the exact same result
(3) as will the original "sum2()" function! If you didn't guess yet, the "extends"
does this. "extends" inherits all the values of one class for use in a completely
different class. IMPORTANT - the name that follows "extends" is *always* the inherited
class. In our example, the "dummy" class is inherited. Therefore, we can access
"$this->sum()" and everything else from the "dummy" class. How cool is that?
<?
class dummy {
var $variable;
var $variable2 = 1;
var $variable3 = 2;
function sum($one, $two) {
$val = $one + $two;
return $val;
}
function sum2() {
$val = $this->sum($this->variable2, $this->variable3);
return $val;
}
}
class dummy3 extends dummy {
function dummy3($variable1, $variable2) {
$val = $this->sum($variable1, $variable2);
return $val;
}
?>
So what's so special about that? What makes "dummy3" special is how you call it. You'd
do it like this:
<?
include("/path/to/file");
$1 = 1;
$2 = 2;
$sum = new dummy3($1, $2);
?>
See how you can pass arguments right inside of your "new dummy3()" call? That's what's
special about this. Though it might seem rather pointless, this is great to use for
functions you use repetitively and want to make calls to with fewer lines of code.
"$sum" will now return 3 if you did "echo $sum" by the way.
<?
class dummy4 {
function test() {
echo "I am from class dummy4.";
}
}
class dummy5 extends dummy4 {
function test() {
echo "I am from class dummy5.";
dummy4::test();
}
}
?>
Now, if you were to call the "test()" function from "dummy5" you would see this:
<?
class dummy6 {
function test() {
echo "I am from class dummy 6.";
}
}
class dummy7 extends dummy6 {
function test() {
echo "I am from class dummy 7.";
parent::test();
}
}
?>
This will return:
<?
class dummy8 {
function dummy8($text) {
echo $text;
}
}
function clone() {
echo $this->dummy8($this->text);
}
}
?>
Then you would do this:
<?
include("/path/to/file");
$text = "Hey there.";
$class = new dummy8($text);
echo $class->clone();
?>
Can you see what's going to happen? "$class->clone()" will print:
Copyright © 2000-2008 Spoono, LLC. All rights reserved.
Network: Reseller Web Hosting by Spoono Host | Spoonloads | Absolute Cross
Terms of Service | Privacy Policy.