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

一个新手
Release: 2023-03-16 13:20:01
Original
1223 people have browsed it

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();
Copy after login

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;
    ?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template