Blogger Information
Blog 13
fans 0
comment 0
visits 8914
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
创建类 创建方法 实例化类 构造方法
文永
Original
1472 people have browsed it

创建类 创建方法 实例化类

实例

<?php
class  work{
    public $name = '肃宁工区';
    public $address = '河北沧州';
    public $telephone = '5088888';

    public function getInfo1(){
        $obj = new self();
            return '工区:' .$obj->name . ',地址:' . $obj->address . ',电话:' .$obj->telephone . '<br>';
    }

    public function getInfo2(){
        return '工区:' . $this->name .',地址:' .$this->address .'<br>';
    }
}

$sn = new work();
echo  $sn->name;
echo '<hr>';
echo $sn->getInfo1();
echo '<hr>';
echo $sn->getInfo2();
echo '<hr>';
$methods = get_class_methods('work');
echo '<pre>'.print_r($methods,ture);
echo '<hr>';

构造方法

实例

<?php
class  work {
    public $name;
    public $address;
    public $telephone;

    public function __construct($name,$address,$telephone)
    {
        echo '开始执行';
        echo '<hr>';
        $this->name = $name;
        $this->address = $address;
        $this->telephone = $telephone;
    }
    public function getInfo()
    {
        return '工区:' . $this->name . ',地址:' . $this->address . ',电话:' . $this->telephone .'<br>';
    }

}

$sn = new work('肃宁工区','河北沧州','13888888');
echo $sn->getInfo();
image.png

image.png

Correcting teacher:查无此人查无此人

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!