相同类、不同类调用变量、常量及方法

<?php
namespace app\admin\controller;
use think\Controller;

class Test extends controller{
const name='PHP软件开发工程师';
private $a='1';

public function index($kgs)
{
$b=$kgs*2;
return $b;
}


public function test(){
echo self::name; //相同类中调用常量
echo $this->a; //相同类中调用变量
echo $this->index(3); //相同类中调用方法
}
}


$b = new Test(); //在其他类中调用Test类
echo $b->index(3); //在其他类中调用Test类index方法.
// echo Test::index(3); //与上面一样的效果
echo(Test::name); //在其他类中调用Test类中的常量
posted @ 2020-02-05 12:55  85541585  阅读(375)  评论(0编辑  收藏  举报