Home  >  Article  >  Backend Development  >  How to instantiate a class in php

How to instantiate a class in php

(*-*)浩
(*-*)浩Original
2019-09-19 15:48:563907browse

The instantiation of a class is an object. A class can be divided into two parts. One is the static description, which is the member attributes in the class. The second is dynamic description, which is the member method in the class, which is the function of the object.

How to instantiate a class in php

# To declare a class, you can add some keywords before class, such as abstract or final, etc.

When declaring a variable in a class, add a keyword in front of it, usually var, as well as public, private, static, and other keywords.

The format of the instantiated object is (Recommended learning: PHP programming from entry to proficiency)

$变量名 = new 类名();   //括号里可传参数

Assigns values ​​to the member attributes in the object The format is

$引用名 ->成员属性 = 值;

The format for calling member methods in the object is

$引用名 -> 成员方法;

The format for using member attributes by member methods is

$this -> 成员属性;
<?php
    class Person{
        var $name;
        var $age;
        var $sex;
         
        function walk(){
            echo $this ->name." is walking.";
        }
    }
     
    $Boy = new Person();
    $Boy ->name = "John";
    $Boy -> walk();
?>

The above is the detailed content of How to instantiate a class in php. 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