Alright guys, I did this for the kirupa tutorials… but I don’t know if it’s good enough… well check it out here… tell me what to change please… I did this on my computer programming class because I had nothing to do.
[FONT=Tahoma]When you start using PHP a lot and you are tired of doing the same thing over, it will be good it you start using Object Oriented Programming (OOP). This will save you a lot of time since you will be using the same code every time. OOP consists of classes and functions mostly. [/FONT]
[FONT=Tahoma]Example of a function:[/FONT]
Function printName($firstname, $lastname) {
$name = $firstname." ".$lastname;
Echo $name."<br>";
}
//Calling the printName function
printName("Kirupa");
[FONT=Tahoma]That would print: “Kirupa”. This is very useful because you have to do less work when you have to echo something. Trust me, when you start using PHP more often, you will come up with classes for everything you do. Now, that’s a function, but we want to put this function inside a class so we can call it in a PHP file and can hold more than one function.[/FONT]
[FONT=Tahoma]Example of a class:[/FONT]
[FONT=Tahoma]
<?
Class myClass {
Var $name;
Var $email;
Var $location;
Function printBio() {
Echo "Hi my name is {$this->$name}. I live in {$this->$location}. If you have any questions, please e-mail me at {$this->$email}.";
}
}
?>
[/FONT]
[FONT=Tahoma]Make sure you save this in a single PHP file.[/FONT]
[FONT=Tahoma]Making it work:[/FONT]
[FONT=Tahoma]
<?
Include "myClassFile.php";
$bio = new myClass;
$bio->name = "Kirupa";
$bio->email = "coolkirupa@yahoo.com";
$bio->location = "U.S.";
$bio->printBio();
?>
[/FONT]
[FONT=Tahoma]This will go on another PHP file, like names.php.[/FONT]
[FONT=Tahoma]Alright, now I will break down the code. [/FONT]
[FONT=Tahoma]Class myClass {[/FONT][FONT=Tahoma]
States a class and it’s name.[/FONT]
[FONT=Tahoma]Var $name;[/FONT]
[FONT=Tahoma]Var $email;[/FONT]
[FONT=Tahoma]Var $location;[/FONT]
[FONT=Tahoma]These are variables that will be used in the class or functions.[/FONT]
[FONT=Tahoma]Function printBio() {[/FONT]
[FONT=Tahoma]Echo “Hi my name is {$this->name}. I live in {$this->location}. If you have any questions, please e-mail me at {$this->email}.”;[/FONT]
[FONT=Tahoma]}[/FONT]
[FONT=Tahoma]Now we will use the variables we defined in the beginning of the class. $this->name is $name. $this means in this class. Name is just how the variable is called. [/FONT]
[FONT=Tahoma]<?[/FONT]
[FONT=Tahoma]Include “myClassFile.php”;[/FONT]
[FONT=Tahoma]Include the file that holds the class, in this case, it’s myClassFile.php.[/FONT]
[FONT=Tahoma]$bio = new myClass;[/FONT]
[FONT=Tahoma]Make $bio our new class.[/FONT]
[FONT=Tahoma]$bio->name = “Kirupa”;[/FONT]
[FONT=Tahoma]$bio->email = "coolkirupa@yahoo.com";[/FONT]
[FONT=Tahoma]$bio->location = “U.S.”;[/FONT]
[FONT=Tahoma]These define the variables name, email, and location to the class used. [/FONT]
[FONT=Tahoma]$bio->printBio();[/FONT]
[FONT=Tahoma]Now this calls the function and prints the biography.[/FONT]
[FONT=Tahoma]?>[/FONT]
[FONT=Tahoma]Hopefully this tutorial was useful to you. This is the first tutorial I’ve ever written so I’m sorry if you get confused. OOP is definitely a PHP facility that helps lazy coders, like me, to get things done faster.[/FONT]
[FONT=Tahoma]Cheers, CriTiCeRz :p:[/FONT]