6Mar/100
Introduction to OOP
First things first, OOP stands for Object Oriented Programming.
A class is a group of functions and variables.
Lets create a class.
In the example's below we are going to create a class which will allow us to display what our favourite movie is.
Code:
class yourclass{
}
This is how we name our class and open it.
Now lets create a variable and function.
Code:
class movie{ //name the class
var $movie = 'none';
function movie($moviename){ //name the function
$this->movie = $moviename; //sets the var movie to the value of moviename
}
}
var $movie = 'none';
function movie($moviename){ //name the function
$this->movie = $moviename; //sets the var movie to the value of moviename
}
}
Now we have our function done. This will set the value of $moviename to $movie, next we will create a function to show it.
Code:
class movie{ //name the class
var $movie = 'none';
function movie($moviename){ //name the function
$this->movie = $moviename; //sets the var movie to the value of moviename
}
function showmovie(){ //name the function
return $this->movie; //return the movie var
}
}
var $movie = 'none';
function movie($moviename){ //name the function
$this->movie = $moviename; //sets the var movie to the value of moviename
}
function showmovie(){ //name the function
return $this->movie; //return the movie var
}
}
This function will show the contents of the $movie variable.
We have still to create the bit that will call our class and then use the functions.
Code:
class movie{ //name the class
var $movie = 'none';
function movie($moviename){ //name the function
$this->movie = $moviename; //sets the var movie to the value of moviename
}
function showmovie(){ //name the function
return $this->movie; //return the movie var
}
}
$mov = new movie;
$mov->movie('Movie name!');
echo $mov->showmovie();
var $movie = 'none';
function movie($moviename){ //name the function
$this->movie = $moviename; //sets the var movie to the value of moviename
}
function showmovie(){ //name the function
return $this->movie; //return the movie var
}
}
$mov = new movie;
$mov->movie('Movie name!');
echo $mov->showmovie();
And we are done! You have created you're first class which will spectify your favourite movie and return it.
Related posts:
- Developers Introduction To AJAX Technology
- Check whether your server is up or down
- Form Validation
- Simple Email Validation
- Internet Explorer CSS bug fixes
Print This Post
