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

How to instantiate a class in php

王林
王林forward
2020-01-25 21:11:532920browse

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

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

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

The format for calling member methods in an object is

$引用名 -> 成员方法;

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

$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();
?>

: 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!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete