Blogger Information
Blog 53
fans 4
comment 3
visits 41396
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php之命名空间用法0508
有点凉了
Original
778 people have browsed it

123.gif

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/9 0009
 * Time: 下午 2:40
 */

namespace Phone{
    class Phone
    {
        public $kind='';
        public $size=0;
        public $color ='';

        public function __construct($kind,$size,$color)
        {
            $this->kind = $kind;
            $this->size=$size;
            $this->color=$color;
        }

        public function IsPhone(){
            return "这个是".$this->kind." size ".$this->size." color ".$this->color;
        }
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/9 0009
 * Time: 下午 2:42
 */

namespace MobilePhone{
    class SelfPhone
    {
        private $selfName='';
        private $age=0;
        const size = "摩托罗拉";
        function __construct($selfname,$age)
        {
            $this->selfName = $selfname;
            $this->age = $age;
        }
        public function goTelPhone(){
            return $this->selfName."去打电话了他的年龄".$this->age."手机是".SelfPhone::size;
        }

        /**
         * @return string
         */
        public function getSelfName()
        {
            return $this->selfName;
        }

        /**
         * @param string $selfName
         */
        public function setSelfName($selfName)
        {
            $this->selfName = $selfName;
            return $this;
        }

        /**
         * @return int
         */
        public function getAge()
        {
            return $this->age;
        }

        /**
         * @param int $age
         */
        public function setAge($age)
        {
            $this->age = $age;
            return $this;
        }

    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/9 0009
 * Time: 下午 2:44
 */
header("content-type:text/html;charset=utf-8");
//spl_autoload_register(function ($class) {//这个为啥不自动提示。。。。 //这个用自动加载怎么加载namespace
//    require_once 'class/' . $class . '.php';
//});//自动加载 这个难道 脚本名称要和类名称完全一致的吗?不然new 类的时候名称不一样 自动加载没法执行了。而且还有as 别名时候自动加载一样不能执行了 应该是这样了。
//测试了下cWarning: require_once(class/Phone\Phone.php): failed to open stream: No such file or directory in这个估计得调整下
require_once "class/Phone.php";
require_once "class/SelfPhone.php";

use Phone\Phone as Phone;//后边可以添加as别名 有这个 new 类的时候 名称要以as 后边为准 没有的as的话 new 以类本身名称为准 as 是为了防止 不同的命名空间中有相同的名称 use进来冲突使用
use MobilePhone\SelfPhone as SelfPhone;

$phone = new Phone("手机", 10, "#BB996C");
echo $phone->IsPhone();
echo "<br>";
$selfPhone = new SelfPhone("QB", 28);
echo $selfPhone->goTelPhone();
$selfPhone->setSelfName("QQ")->setAge("80");
echo "<br>";
echo $selfPhone->getSelfName();
echo "<br>";
echo $selfPhone->goTelPhone();

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post