name
control"; } }"/> name
control"; } }">

Home  >  Article  >  Backend Development  >  About object-oriented PHP - the definition of classes, the instantiation operations of objects, and the special usage of constructors and destructors - WORSHIP Yasa

About object-oriented PHP - the definition of classes, the instantiation operations of objects, and the special usage of constructors and destructors - WORSHIP Yasa

一个新手
一个新手Original
2017-10-04 09:27:051155browse

Class definition and object instantiation operation

<?php 
//类里面的成员是属于对象的
class Hero{
    public $name;//成员变量
    //成员属性(存在于强类型语言中)
    protected $blood;
    private $attack;
    //成员方法
    function skill(){
        echo "$this->name<br>control";        
    }
}
$s = new Hero();//造一个英雄
$s->name = "Riven";  //给英雄赋值 
echo "$s->name<br>";//输出英雄名
$s->skill();

Constructor, destructor

//作用:对类里面的某些成员进行初始化(参数自定义初始值)
class Ren {
    public $name;
    public $age;
    //强类型语言中使用类型
//老版本定义用法 
/* public function Ren() {
    echo "正在构造函数";
    public function Ren($v) {
    $this->age = $v;*/
    //PHP新版本定义用法 
    public function __construct($v) {
    $this->age = $v;
}
public function __destruct() {
    echo "正在销毁文件";
}
}$s = new Ren(20);
    echo $s->age;
    ?>

The above is the detailed content of About object-oriented PHP - the definition of classes, the instantiation operations of objects, and the special usage of constructors and destructors - WORSHIP Yasa. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn