This article introduces to you the difference and use of const and static in php. Friends in need can refer to it
First of all, about const, members can only be modified inside a php class. Attributes cannot modify methods, as follows:
[php] view plain copy
class Test{ const PATH = 'c/';//修饰常量 const function test(){//这种方法是错误的,const无法修饰方法 } }
const is in use There is no need to add permission modified fields, but it requires skills when calling const modified constants, because the constants belong to the entire class, not to a certain object, so you need to use the class name plus the domain name modifier when calling. Example:
[php] view plain copy
echo Test::PATH; //也可以使用下面的方法调用 $test = new Test(); echo $test::PATH;
But there will be problems when calling inside the class , how to get the current class name or current object name inside the class (php can get the class name based on the object - reflection), php provides us with two special keywords, such as:
[php] view plain copy
class Test{ const PATH = 'c/';//修饰常量 public function ass(){ echo $this::PATH.'<br />'; echo self::PATH.'<br />'; } }
where self (without $) represents the current class name, and $this represents the current object. In this way, we can call const modified constants inside the class.
Next let’s talk about static
#Although const is very useful, the value cannot be modified once it is defined, but sometimes we You need an attribute that belongs to the class, but its value can be modified. For example, if you count how many objects of this class have been constructed, it is time to use static. When static modifies the member variables of the class, its usage method is basically the same as const. The same, the difference is that permissions can be added to statically modified attributes (const can also be added in 7.1 PHP, but not in the past), and the values of statically modified member variables can be modified.
But when the member method is statically modified, the usage method changes a little. Inside the class, inside the statically modified method body, any ordinary member variables of the current class cannot be accessed. To be precise Generally speaking, the keyword $this cannot be used, and only static member variables and class constants of the current class can be used.
[php] view plain copy
class Test{ static public $name='asd'; const π = 3.1415926; public $age = 10; public static function lenth($r){ //echo $this->age;//会发生错误,不能访问普通的成员变量 echo Test::$name; return $r*2*self::π; } public static function area($r){ return $r*$r*self::π; } }
In the process of using static, the method called is the same as calling static The modified variables are different and can be called through class names or object names, for example:
[php] view plain copy
echo '周长:'. Test::lenth(1).'<br/>'; echo '面积:'.Test::area(1).'<br/>'; $per = new Test(); echo $per->lenth(3);
The above two calling methods are legal.
Related recommendations:
How to use the iconv function in php
PHP_EOL newline parsing in php
The above is the detailed content of The difference and use of const and static in php. For more information, please follow other related articles on the PHP Chinese website!

In PHP, you can use session_status() or session_id() to check whether the session has started. 1) Use the session_status() function. If PHP_SESSION_ACTIVE is returned, the session has been started. 2) Use the session_id() function, if a non-empty string is returned, the session has been started. Both methods can effectively check the session state, and choosing which method to use depends on the PHP version and personal preferences.

Sessionsarevitalinwebapplications,especiallyfore-commerceplatforms.Theymaintainuserdataacrossrequests,crucialforshoppingcarts,authentication,andpersonalization.InFlask,sessionscanbeimplementedusingsimplecodetomanageuserloginsanddatapersistence.

Managing concurrent session access in PHP can be done by the following methods: 1. Use the database to store session data, 2. Use Redis or Memcached, 3. Implement a session locking strategy. These methods help ensure data consistency and improve concurrency performance.

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools
