1. Introduction to Classes and namespaces
foo.php:
<?php
Require_once ' lib.php ';
$h = new Hello (); Instances of the class
$h->sayhello (); Methods for instance Access classes
S
$d = new Desktop ();
$d work ();
Require_once ' apple/foo.php '; File location for foo.php
$h = new \jkxy\hello (); The foo.php namespace, in the Hello class, namespace Jkxy;
$h->sayhello ();
Require_once ' lib.php ';
$m = new Man (' Iwen ');
echo $m->getname ();
lib.php:
<?php
Class hello{
Public Function SayHello () {
echo ' Hello php ';
// }
// }
Class desktop{
Public function work () {
echo ' work ';
// }
// }
2. Member Methods and class methods, class inheritance and overrides: people.php >> man.php, foo.php call man.php, see man.php Inheritance properties and overriding properties
foo.php:
<?php
Require_once ' people.php ';
for ($i =0; $i <10; $i + +) {
$p = new People (' Iwen ', ' man ');
echo $p->getname (). ' ‘;
// }
Require_once ' man.php ';
$m = new Man (' Iwen ');
$m->hi ();
people.php:
<?php
Class people{
/*
* @param int $age people ' s age
* @param string $name people ' s name
* @param string $sex people ' s sex
*/
Public function __construct ($age, $name, $sex) {
Echo ' Construct people ';
$this->_age = $age;
$this->_name = $name;
$this->_sex = $sex;
People:: $NUM + +;
If (people:: $NUM >people::max_people_num) {
throw new Exception ("You can ' t create more people");
}
}
Public Function Getage () {
return $this->_age;
}
Public Function GetName () {
return $this->_name;
}
Public Function Getsex () {
return $this->_sex;
}
Public Function hi () {
echo $this->_name. ' Say hi ';
}
Private $_age,$_name,$_sex;
private static $NUM = 0;
Const MAX_PEOPLE_NUM = 10;
public static function SayHello () {
Echo ' Hello people ';
}
}
man.php:
<?php
Require_once ' people.php ';
Class Man extends people{
Public function __construct ($age, $name) {
Parent::__construct ($age, $name, ' man ');
}
Public Function hi () {
Parent::hi ();
echo ' man '. $this->getname (). ' Say hi ';
}
}
Introduction to PHP classes and namespaces, member methods and class methods, inheritance of classes and method overrides