Introduction to PHP classes and namespaces, member methods and class methods, inheritance of classes and method overrides

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.