Steven Sun's Blog

A dream land of peace.

Oop in PHP

1. define a class with function and parameters

1
2
3
4
5
6
7
8
9
class classname
    {
    function operation1()
    {
    }
    function operation2($param1, $param2)
    {
    }
}

2. constructor

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
class classname
{
  function __construct($param)
  {
    echo "Constructor called with parameter ".$param."<br />";
  }
}

$a = new classname("First");
$b = new classname("Second");
$c = new classname();
?>

3. class attribute

1
2
3
4
5
6
7
class classname
{
public $attribute;
}
$a = new classname();
$a->attribute = "value";
echo $a->attribute;

4.  When you use this keyword “final” in front of a function declaration, that function cannot be overridden in any subclasses

5.  PHP use “interface” to implement the function of “multi inheritance”

1
2
3
4
5
6
7
8
9
10
11
12
interface Displayable
{
    function display();
}

class webPage implements Displayable
{
    function display()
{
// ...
}
}