php5.5中类级别的常量使用介绍
最新学习了一下php5.5发现有一个新的功能就是类级别的常量,下面我来给大家简单分享一下我的学习笔记
不久前php刚发布了5.5的第一个稳定版,介绍了一个类级别的常量,名字是 `CLASS` 这个常量对所有的类有效,返回类的全名。
复制代码 代码如下:
namespace vendorpackage;
class Foo
{
// ...
}
var_dump(Foo::CLASS);
//上面脚本输出 string(18) "vendorpackageFoo".
为什么要使用它
我们为什么要使用一个这样的常量,当然不是像上面那个例子一样只是获得类的全名。我们使用__NAMESPACE__也可以达到同样的效果,,而且php5.3就可以用了:
复制代码 代码如下:
namespace vendorpackage;
class Foo
{
// ...
}
var_dump(__NAMESPACE__ . 'Foo');
然而,当你需要完全限定名称,命名空间引用了类命名空间别名…然后它变得有趣。
在下面的例子:
复制代码 代码如下:
use vendorpackageFoo;
class FooTest extends PHPUnit_Framework_TestCase
{
public function testBarCanBeProcessed()
{
$bar = $this->getMock('vendorpackageBar');
$foo = new Foo;
$foo->process($bar);
// ...
}
}
复制代码 代码如下:
use vendorpackageFoo;
use vendorpackageBar;
class FooTest extends PHPUnit_Framework_TestCase
{
public function testBarCanBeProcessed()
{
$bar = $this->getMock(Bar::CLASS);
$foo = new Foo;
$foo->process($bar);
// ...
}
}

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



A constant is also called a variable and once defined, its value does not change during the execution of the program. Therefore, we can declare a variable as a constant referencing a fixed value. It is also called text. Constants must be defined using the Const keyword. Syntax The syntax of constants used in C programming language is as follows - consttypeVariableName; (or) consttype*VariableName; Different types of constants The different types of constants used in C programming language are as follows: Integer constants - For example: 1,0,34, 4567 Floating point constants - Example: 0.0, 156.89, 23.456 Octal and Hexadecimal constants - Example: Hex: 0x2a, 0xaa.. Octal

Constants and variables are used to store data values in programming. A variable usually refers to a value that can change over time. A constant is a type of variable whose value cannot be changed during program execution. There are only six built-in constants available in Python, they are False, True, None, NotImplemented, Ellipsis(...) and __debug__. Apart from these constants, Python does not have any built-in data types to store constant values. Example An example of a constant is demonstrated below - False=100 outputs SyntaxError:cannotassigntoFalseFalse is a built-in constant in Python that is used to store boolean values

A constant variable is a variable whose value is fixed and only one copy exists in the program. Once you declare a constant variable and assign a value to it, you cannot change its value again throughout the program. Unlike other languages, Java does not directly support constants. However, you can still create a constant by declaring a variable static and final. Static - Once you declare a static variable, they will be loaded into memory at compile time, i.e. only one copy will be available. Final - Once you declare a final variable, its value cannot be modified. Therefore, you can create a constant in Java by declaring the instance variable as static and final. Example Demonstration classData{&am

PHP is a server-side scripting language widely used in web development. Its flexibility and ease of use make it the first choice for many developers. However, when using PHP, we sometimes encounter some error reports. This article will focus on the "Call to undefined constant" error and how to resolve it. 1. Problem Description When we use an undefined constant in the code, PHP will throw a fatal error, prompting us to call an undefined constant. Here is a common example: echoMY_

PHP error: What should I do if I use an undefined constant as a property name? In PHP development, we often use classes and objects to organize and manage code. In the process of defining a class, the attributes of the class (i.e. member variables) play an important role in saving data. However, when we use properties, sometimes an error occurs when using undefined constants as property names. This article explains the causes of this error and provides several solutions. First, let's look at a simple example to demonstrate this problem. Suppose we have a file named "Per

To learn the definition and initialization method of basic data type constants, specific code examples are required. In programming, various basic data types are often used, such as integers, floating point types, character types, etc. When using these data types, you not only need to understand their definition and usage, but also how to define and initialize their constants. This article will introduce you to the definition and initialization method of basic data type constants, and give specific code examples. Definition and initialization method of integer constants Integer constants include int, long, short and byt

The FILTER_SANITIZE_SPECIAL_CHARS constant filters HTML escaped special characters. Flags FILTER_FLAG_STRIP_LOW − Strip characters with ASCII values below 32 FILTER_FLAG_STRIP_HIGH − Strip characters with ASCII values above 32 FILTER_FLAG_ENCODE_HIGH − Encode characters with ASCII values above 32 Return value FILTER_SANITIZE_SPECIAL_CHARS constant does nothing. Example Demo&

Naming conventions in PHP: How to use underscore nomenclature to name constants and file names In PHP programming, good naming conventions are very important to improve the readability and maintainability of the code. This article will introduce how to use underscore nomenclature to name constants and file names, and demonstrate it with code examples. Naming convention for constants In PHP, constants are usually named in all uppercase letters, with words separated by underscores. This naming convention clearly distinguishes constants from variables and is easy to read and understand. Here are some common constant naming examples: def
