Sunday, November 14, 2010
OOP in PHP
Let's take a look at creating classes in PHP.
The above example creates a class entitled "MyClass." Whenever you create a new instance of MyClass, the constructor is called immediately. We define the variable 'lol' in the class, and this variable can be used by any method inside of the class.
How are classes useful? You can create classes to handle all sorts of different things for your PHP projects. Package all of your methods into classes that can be used over and over again in different parts of your site.
<?php
class MyClass
{
var lol;
public function __construct()
{
$this -> lol = 'hit me!';
}
public function display()
{
echo $this -> lol;
}
}
?>
class MyClass
{
var lol;
public function __construct()
{
$this -> lol = 'hit me!';
}
public function display()
{
echo $this -> lol;
}
}
?>
The above example creates a class entitled "MyClass." Whenever you create a new instance of MyClass, the constructor is called immediately. We define the variable 'lol' in the class, and this variable can be used by any method inside of the class.
How are classes useful? You can create classes to handle all sorts of different things for your PHP projects. Package all of your methods into classes that can be used over and over again in different parts of your site.
Subscribe to:
Comments (Atom)





