Home > Backend Development > PHP Tutorial > PHP object-oriented classes and instantiated objects

PHP object-oriented classes and instantiated objects

不言
Release: 2023-03-29 22:00:01
Original
1896 people have browsed it

This article mainly introduces PHP object-oriented classes and instantiated objects, which has a certain reference value. Now I share it with everyone. Friends in need can refer to it

Class

Declaration

[修饰符] class 类名
{
    [属性]
    [方法]
}
Copy after login

Notes:

1) The class name follows the camel case naming convention that starts with uppercase

2) The opening and closing tags of curly braces must be on a line of their own.

Modifier

private     私有

protected   保护

public      公共    

var         被视为public  (不建议使用)
Copy after login

Access permission

PHP object-oriented classes and instantiated objects

Instantiation

Use the new keyword to instantiate.

Brackets are not required.

Example

// 1.声明类
class Dog
{
    // 2.定义属性
    public $name = '旺财';
    public $sex = null;


    // 3.定义方法
    public function bark()
    {
        echo '汪汪~';
    }
}

// 4.实例化
$d1 = new Dog();
Copy after login

Instantiated object

Invocation of properties and methods

Use

-> , to access non-static properties | methods.

Example

// 声明类
class Dog
{
    // 定义属性
    public $name = '旺财';
    public $sex = null;


    // 定义方法
    public function bark()
    {
        echo '汪汪~';
    }
}

// 实例化
$d1 = new Dog();

// 使用属性和方法
echo $d1 -> name;
echo &#39;<br/>&#39;;
echo $d1 -> bark();
Copy after login

Internal call

When a method is called inside a class definition, you can use pseudo variables

$this .

$this represents the object being used.

Example:

// 声明类
class Dog
{
    // 定义属性
    public $name = &#39;旺财&#39;;
    public $sex = null;

    public function intruduce()
    {
        echo &#39;我的名字是&#39;.$this->name;
    }
}
Copy after login

Transfer assignment

1)

Simple type, both sides of the assignment, mutually independent.

2) When

composite type, the identifier of the object is stored in the transfer assignment, so the changes are consistent .

Simple type example:

$a = &#39;abc&#39;;

// 传递赋值
$b = $a;

// 更改a的值
$a = &#39;qq&#39;;

var_dump($a);   // qq
var_dump($b);   // abc
Copy after login

Composite type example:

class MyClass
{
    public $str = &#39;abc&#39;;
}

// $a 存的是对象的标识符
$a = new MyClass();

// 传递赋值  相当于把标识符赋值给了$b
$b = $a;

var_dump($a);    // abc
var_dump($b);    // abc

// 更改a对象str属性的值
$a -> str =&#39;111&#39;;

// $a和$b存的都是标识符
var_dump($a);    // 111
var_dump($b);    // 111


// 更改a本身的值
$a = 123;

var_dump($a);    // 123
var_dump($b);    // 111   对象标识符
Copy after login

Reference assignment

Whether it is Whether it is a simple type or a conforming type, both changes are consistent.

Example:

$a = &#39;abc&#39;;

// 引用赋值
$c = &$a;

var_dump($a);   // qq
var_dump($c);   // qq
Copy after login
Related recommendations:

php basic object-oriented concepts

The above is the detailed content of PHP object-oriented classes and instantiated objects. 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