How to instantiate a class in php

王林
Release: 2023-02-28 18:14:02
forward
2985 people have browsed it

How to instantiate a class in php

The instantiation of a class is actually an object.

A class can be divided into the following two parts:

1. Static description, which is the member attributes in the class;

2. Dynamic description, which is the member methods in the class , that is, the function of the object.

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

Free learning video tutorial recommendation: php video tutorial

When declaring variables in a class, add a keyword in front, usually var, and public, private , static, and other keywords.

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

The format for assigning values ​​to member properties in an object is

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

The format for calling member methods in an object is

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

The format for member methods to use member properties Recommended articles and tutorials for

$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

: php tutorial

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:
source:jb51.net
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