Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * This Person class encapsulates a couple of properties which
4
 * a person might have: their name and age.
5
 * We also give the Person the opportunity to introduce themselves.
6
 *
7
 * @link http://cowburn.info/php/php5-method-chaining/
8
 */
9
class Person
10
{
11
    var $m_szName;
12
    var $m_iAge;
13
 
14
    function setName($szName)
15
    {
16
        $this->m_szName = $szName;
17
        return $this; // We now return $this (the Person)
18
    }
19
 
20
    function setAge($iAge)
21
    {
22
        $this->m_iAge = $iAge;
23
        return $this; // Again, return our Person
24
    }
25
 
26
    function introduce()
27
    {
28
        printf('Hello my name is %s and I am %d years old.',
29
            $this->m_szName,
30
            $this->m_iAge);
31
    }
32
}
33
 
34
// We'll be creating me, digitally.
35
$peter = new Person();
36
// Let's set some attributes and let me introduce myself,
37
// all in one line of code.
38
$peter->setName('Peter')->setAge(23)->introduce();
39
?>