Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 340
0 Users 7 Guests Online

OOP, yep, OOP

This tutorial will teach you OOP (Object Oriented Programming) in PHP. You should have basic PHP knowledge before reading this tutorial. First I will teach you how to make a class, and use it within PHP.

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
//
//    Build the class, replace ClassName, with the name of your class
//
class ClassName {

//
//    Set a variable, will be set to a value later
//
var $variable;

//
//    Create a function to print out the variable
//
  
function myfunction() {
  print(
$this->variable);
  }
  
//
//    End the class
//
}
?>


This will create a class, but will not really do anything until you build it later. Now, for a little explanation, the variables set in classes need not be set to a value, because later in the script you can set it from outside the class block. You may have of noticed the $this->variable statement. When using classes, you use relation to that class, and variables you want to use while you have code in the class, you can use the $this->{VARIABLENAME}. You may have of also noticed that there is no $ in front of variable, classes don't work like that. Now I can show you how to build a class from outside, and print out the variable.

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
//
//    Build the class, replace ClassName, with the name of your class
//
class ClassName {

//
//    Set a variable, will be set to a value later
//
var $variable;

//
//    Create a function to print out the variable
//
  
function myfunction() {
  print(
$this->variable);
  }
  
//
//    End the class
//
}

//
//    Create the object
//
$build=new ClassName;

//
//    Set the variable within the object (notice no $ in front of variable)
//
$build->variable="Hello, this is some text.";

//
//    Call the function from within the object
//
$build->myfunction();
?>


This will print out: Hello, this is some text.

The $build variable (after creating the object through it) now relates everything to it with the class, so you could also do it like this:

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
//
//    Build the class, replace ClassName, with the name of your class
//
class ClassName {

//
//    Set a variable, will be set to a value later
//
var $variable;

//
//    Create a function to print out the variable
//
  
function myfunction() {
  print(
$this->variable);
  }
  
//
//    End the class
//
}

//
//    Create the object
//
$build=new ClassName;

//
//    Set the variable within the object (notice no $ in front of variable)
//
$build->variable="Hello, this is some text.";

//
//    Call the variable from within the object
//
echo $build->variable;
?>


Will print out the same thing as the previous example. For a better understanding of the functions inside of a class, here is an example of some math:

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
//
//    Build the class, replace ClassName, with the name of your class
//
class ClassName {

//
//    Set a variable, will be set to a value later
//
var $variable;

//
//    Create a function to print out the variable
//
  
function add($varA,$varB) {
  
$this->variable=$varA+$varB;
  print(
$this->variable);
  }
  
//
//    End the class
//
}

//
//    Build the object
//
$build=new ClassName;

//
//    Call to the function within the object
//
$build->add(5,5);
?>


As you probably may have guessed, this will print out 10. Just like a normal function in PHP, except called by the object using the $build variable, then stores the answer in the $variable variable. This means you can do this:

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
//
//    Build the class, replace ClassName, with the name of your class
//
class ClassName {

//
//    Set a variable, will be set to a value later
//
var $variable;

//
//    Create a function to print out the variable
//
  
function add($varA,$varB) {
  
$this->variable=$varA+$varB;
  print(
$this->variable);
  }
  
//
//    End the class
//
}

//
//    Build the object
//
$build=new ClassName;

//
//    Call to the function within the object
//
$build->add(5,5);

//
//    Call the variable, which now has the value of 5+5 in it
//
echo "".$build->variable;
?>


This will print out:
10
10

Now, you can use the extends method in OOP, so one class extends into another class, obtaining all of it's functions, variables, etc..

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
//
//    Build the class, replace ClassName, with the name of your class
//
class ClassName {

//
//    Set a variable
//
var $variable;

//
//    Create a function to do something
//
  
function dosomething() {
  
//
  //    Reset the variable to a new value (change e's to 1's)
  //
  
$this->variable=ereg_replace("e","3",$this->variable);
  
  
//
  //    Print out the variable
  //
  
print($this->variable);
  }

//
//    End class
//
}

//
//    Build the class, you can rename it, nothing has to be put in here
//
class DiffClassName extends ClassName {

//
//    End the class
//
}

//
//    Build the DiffClassName object
//
$build=new DiffClassName;

//
//    Set the variable to something
//
$build->variable="There are a lot of e's in this sentence eh?";

//
//    Run the function from the object
//
$build->dosomething();
?>


That should print out:
Th3r3 ar3 a lot of 3's in this s3nt3nc3 3h?

As you can see, the function in the first class, will replace the e's with 3's. You may notice that we built the DiffClassName object, instead of the ClassName object to output the sentence. This shows us that you can use the extends method to output data obtained from the parent class. This is all for my tutorial.
RossT
Author:
Views:
2119
Rating:
There are currently no comments for this tutorial.