The difference between static attributes and static methods in php

墨辰丷
Release: 2023-03-27 18:16:02
Original
1539 people have browsed it

This article mainly introduces the detailed explanation of the difference between PHP static properties and static methods. Friends who need it can refer to

static

public, private, protected

Static properties

Adding the static keyword before a property or method is a static property

In actual work, there will be multiple objects of a class , a copy of the data may be shared.

Solution:

1: Class constant, cannot be changed.

Example:

2: Sometimes after sharing a piece of data, all objects sharing this data must also allow changes.

So there is static

Syntax:

Example:

Tip: Static properties are similar (identical) to class constants. The only difference is that class constants cannot be changed, while static properties can be changed. The access method is the same.

:: You can only access class constants, static properties, and static methods

Static properties need to add $. There is no $ before the constant name, so you don’t need to add it at all when accessing class constants.

-> There is no need to add $

static method

Add static before the method, it is a static method

Access to static members: Access to static properties

Public static properties are accessed outside the class:

Syntax:

Class name::static Property name

Access all static properties within the class

##Static method access

Outside-class access

Syntax:

Static method exists in class space.

Class name::Method name();     

##Intra-class access:

Syntax:

self: :Method name();

The difference between static and non-static:

Static properties are saved in the class space, non-static properties Saved in the object space

Static properties are accessed through class access(::), non-static properties are accessed through object(->)

Static methods are accessed through class access( ::),

If a method does not operate the properties belonging to the object space internally, it will be encapsulated into a static method and called through the class. This method will be much faster.

$this cannot appear in static methods

*******

##Important note:

If it belongs to a class, use the class name or self::, if it belongs to an object, use the object name or $this->. (Except ordinary methods==>Although ordinary methods belong to the class space, they still need to be accessed using $this or the object name)Magic method

php output statement:

echo A grammar structure

can only output the scalar data type, and there is no output for Boolean value. The value false has no output

print_r() Not only can it output scalar data types, it can also output data. There is no output for the boolean value false

var_dump($v1,$v2); More output variables The information is mainly for programmers to debug programs using

__tostring();

When echoing an object, it means converting a structure into a string.

__tostring() magic method is called by the PHP automatic execution mechanism when an object is to be converted into a string due to a certain operation.

This method is to capture errors (Fatal Erorr fatal errors) and output a prompt message

Example:

__clone();

clone(); Clone function

##The above is the entire content of this article, I hope it will be useful for everyone’s learning help.


Related recommendations:

thinkphp random string

thinkphp3.2.3 registration Upload pictures

thinkphp3.2.3 complete paging example

The above is the detailed content of The difference between static attributes and static methods in php. 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