Home Backend Development PHP Tutorial 懂得PHP5中static和const关键字

懂得PHP5中static和const关键字

Jun 13, 2016 am 10:49 AM
counter function self static

理解PHP5中static和const关键字

PHP5中加入了很多面向对象的思想,PHP5的面向对象比较接近Java的面向对象思想。我们这里对PHP5中的static和const关键字作用进行一下描述,希望对学习PHP5的朋友有帮助。

(1) static
static关键字在类中是,描述一个成员是静态的,static能够限制外部的访问,因为static后的成员是属于类的,是不属于任何对象实例,其他类是无法访问的,只对类的实例共享,能一定程序对该成员尽心保护。类的静态变量,非常类似全局变量,能够被所有类的实例共享,类的静态方法也是一样的,类似于全局函数。类的静态方法能访问类的静态的属性。另外说明的是,static的成员,必须使用self来访问,使用this会出错
(关于this和self的异同,请参考:?http://blog.csdn.net/heiyeshuwu/archive/2004/11/03/165828.aspx?)

(2)const
const是一个定义常量的关键字,类似于C中的#define,能够定义一个常量,如果在程序中改变了它的值,那么会出现错误。

举例说明上面的代码

<?phpclass Counter{	private static $count = 0;//定义一个静态属性	const VERSION = 2;//定义一个常量	const ACTIVED = 1;//定义一个常量	//构造函数	function __construct(){		self::$count++;	}	//析构函数	function __destruct(){		self::$count--;	}	//定义一个静态的方法	static function getCount(){		return self::$count;	}		function get_info($code=''){		$info = array(			self::VERSION=>'版本',			self::ACTIVED=>'激活', 		);		return isset($info[$code]) ? $info[$code] : $info;	}}//创建一个实例$c = new Counter();//执行打印print( Counter::getCount(). "<br>\n" ); //使用直接输入类名来访问静态方法Counter::getCount//打印类的版本print( "Version useed: " .Counter::VERSION. "<br>\n" );?>
Copy after login
?
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Why NameResolutionError(self.host, self, e) from e and how to solve it Why NameResolutionError(self.host, self, e) from e and how to solve it Mar 01, 2024 pm 01:20 PM

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

How to use self in Python How to use self in Python May 17, 2023 pm 10:40 PM

Before introducing the usage of self in Python, let’s first introduce the classes and instances in Python. We know that the most important concepts of object-oriented are classes and instances. Classes are abstract templates, such as abstract things like students. , can be represented by a Student class. Instances are specific "objects" created based on classes. Each object inherits the same methods from the class, but its data may be different. 1. Take the Student class as an example. In Python, the class is defined as follows: classStudent(object):pass(Object) indicates which class the class inherits from. The Object class is all

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

How to use the Counter module in Python How to use the Counter module in Python Apr 19, 2023 pm 02:55 PM

Description Project Description Python Interpreter 3.10.6 Counter module In Python's collections module, a very commonly used module is Counter. Counter is a simple counter used to count the number of certain hashable objects. It stores elements and their counts in the form of a dictionary. Counter() class Counter() can count the parameters passed to this class according to certain rules, and return the results in the form of a dictionary by using the counting object and the counting result as key-value pairs. Counter(iterable=None,/,**kwds) gives an example fromcollectionsimport

clearstatcache() function in PHP clearstatcache() function in PHP Sep 07, 2023 am 09:33 AM

The clearstatcache() function is used to clear the file status cache. PHP caches the information returned by the following functions −stat()lstat()file_exists()is_writable()is_readable()is_executable()is_file()is_dir()filegroup()fileowner()filesize()filetype()fileperms() What to do To provide better performance. Syntax voidclearstatecache() Parameter NA Return value clearstatcache(

The usage and function of Vue.use function The usage and function of Vue.use function Jul 24, 2023 pm 06:09 PM

Usage and Function of Vue.use Function Vue is a popular front-end framework that provides many useful features and functions. One of them is the Vue.use function, which allows us to use plugins in Vue applications. This article will introduce the usage and function of the Vue.use function and provide some code examples. The basic usage of the Vue.use function is very simple, just call it before Vue is instantiated, passing in the plugin you want to use as a parameter. Here is a simple example: //Introduce and use the plug-in

See all articles