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

<?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类中的常量

猜你喜欢

转载自www.cnblogs.com/fuxp/p/12263295.html
今日推荐