Home > Backend Development > PHP Problem > How to instantiate a class in php

How to instantiate a class in php

(*-*)浩
Release: 2023-02-24 13:12:01
Original
3953 people have browsed it

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 类名();   //括号里可传参数
Copy after login

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

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

The format for calling member methods in the object is

$引用名 -> 成员方法;
Copy after login

The format for using member attributes by member methods is

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

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!

Related labels:
php
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