Blogger Information
Blog 11
fans 0
comment 1
visits 8249
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
初入对象0929
梦惊禅的博客
Original
643 people have browsed it
<?php
namespace total{
//1. 自写案例演示匿名函数的三个应用场景
//(1)、函数赋值给一个变量
    $func = function(){
        return "my name is zgj<br>";
    };

    echo $func();

//(2)、函数直接作为形参
    function index($func){
        $func();
    }

    index(function (){
        echo "函数直接作为形参\n";
    });

//(3)、闭包
    $name = 'zgj';
    function getName($callback){
        $callback();
    }

    echo "局部变量".getName(function()use($name){
            return $name;
        })."<br>";
}



//2. 深刻理解全名空间的意义,并实例演示多个命名空间的场景
namespace {
    class Person{

    }
}

//3. 实例演示类与对象的关系与使用方式
namespace oop{
    class Person{

        private $name;
        private $age;
        private $sex;

        function __construct($name,$sex,$age)
        {
            $this->name = $name;
            $this->sex = $sex;
            $this->age = $age;
        }

        /**
         * @return mixed
         */
        public function getName()
        {
            return $this->name;
        }

        /**
         * @param mixed $name
         */
        public function setName($name): void
        {
            $this->name = $name;
        }

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

        /**
         * @param mixed $age
         */
        public function setAge($age): void
        {
            $this->age = $age;
        }

        /**
         * @return mixed
         */
        public function getSex()
        {
            return $this->sex;
        }

        /**
         * @param mixed $sex
         */
        public function setSex($sex): void
        {
            $this->sex = $sex;
        }
    }

    $per = new Person('zgj','M',22);
    echo "姓名 ".$per->getName()." 年龄 ".$per->getAge()." 性别  ".$per->getSex()."<br>";

}

//4. 总结命名空间的应用对象, 适用场景以及注意事项
//命名空间的作用是为解决重名问题,将代码划分为不同的区域,每个区域的常量、函数和类的名字互不影响。
//其他暂时没想法


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