Home > Backend Development > PHP Problem > How to use constructor and this keyword in PHP? How to introduce constructor?

How to use constructor and this keyword in PHP? How to introduce constructor?

慕斯
Release: 2023-03-10 17:04:01
Original
1635 people have browsed it

The previous article introduced you to "What are classes and objects in PHP? Why learn object-oriented? how to use? 》, this article continues to introduce to you how to use the constructor and this keyword in PHP? How to introduce constructor? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to use constructor and this keyword in PHP? How to introduce constructor?

Construction method and this keyword:

Introducing the construction method

Create When creating an object, we need to initialize the object. At this time, we need to call our constructor. The constructor is automatically called, not manually. It is a magic method

__construct ()
Copy after login
  • Constructor without passing parameters

  • Create object and call directly|

  • Constructor with parameters passed

  • ## Assign the passed parameters to your own member properties

  • In the class, you need to access your own member properties and member methods

  • $this:当前对象,
    $this->name ;
    $this->cook() ;
    Copy after login
First we Create a new file, first write a class, person, and then give it two attributes, one is poetry and the other is pome, then if we want to create an object now, we can $ming, if we want to access poetry in ming

We take the code as an example:

<?php
class Person {
    public $poet =&#39;林徽因&#39;;
    public $pome= &#39;答案很长,我准备用一生的时间来回答,你准备要听了吗?&#39;;
}
$ming = new Person();
var_dump( $ming);
echo &#39;<br>&#39;;
$niu = new Person();
var_dump ($niu);
?>
Copy after login

The code displays the result:

How to use constructor and this keyword in PHP? How to introduce constructor?

We can see from the results that the displays are the same, so It does not conform to our logic. Our objects correspond to their respective contents, so we can write like this:

<?php
class Person
{
    public $poet;
    public $pome;
}
$ming = new Person();
$ming->poet = &#39;林徽因&#39; ;
$ming->pome = &#39;答案很长,我准备用一生的时间来回答,你准备要听了吗?&#39;;
var_dump ( $ming);
echo &#39;<br>&#39;;
$niu = new
Person();
$niu->name = &#39;张爱玲&#39;;
$niu->age = &#39;你还不来,我怎敢老去&#39;;
var_dump($niu);
Copy after login
The code displays the result:

How to use constructor and this keyword in PHP? How to introduce constructor?

Recommended Learn:

php video tutorial

The above is the detailed content of How to use constructor and this keyword in PHP? How to introduce constructor?. 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