国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 PHP如何實(shí)現(xiàn)定義對象類型的常量

PHP如何實(shí)現(xiàn)定義對象類型的常量

Jun 06, 2016 pm 07:35 PM
DE php how definition accomplish object constant normal type

正常的直接define()一個對象,是回報錯的,但我們查看PHP源碼,可以發(fā)現(xiàn)除了數(shù)組不允許外,其他類型都可以用來定義常量,只不過對象的話要轉(zhuǎn)化。 我們只需使用tostring方法即可。 無 repeat:switch (Z_TYPE_P(val)) {case IS_LONG:case IS_DOUBLE:case IS_STR

正常的直接define()一個對象,是回報錯的,但我們查看PHP源碼,可以發(fā)現(xiàn)除了數(shù)組不允許外,其他類型都可以用來定義常量,只不過對象的話要轉(zhuǎn)化。
我們只需使用tostring方法即可。
repeat:
	switch (Z_TYPE_P(val)) {
		case IS_LONG:
		case IS_DOUBLE:
		case IS_STRING:
		case IS_BOOL:
		case IS_RESOURCE:
		case IS_NULL:
			break;
		case IS_OBJECT:
			if (!val_free) {
				if (Z_OBJ_HT_P(val)->get) {
					val_free = val = Z_OBJ_HT_P(val)->get(val TSRMLS_CC);
					goto repeat;
				} else if (Z_OBJ_HT_P(val)->cast_object) {
					ALLOC_INIT_ZVAL(val_free);
					if (Z_OBJ_HT_P(val)->cast_object(val, val_free, IS_STRING TSRMLS_CC) == SUCCESS) {
						val = val_free;
						break;
					}
				}
			}
			/* no break */
		default:
			zend_error(E_WARNING,"Constants may only evaluate to scalar values");
			if (val_free) {
				zval_ptr_dtor(&val_free);
			}
			RETURN_FALSE;
	}
Class Test{
   public $name='呵呵';
   public function __toString(){
     return $this->name;
   }
}

$t=new Test();
define('TT',$t);
echo TT;
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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

Documenting PHP with Comments Documenting PHP with Comments Jul 18, 2025 am 04:34 AM

When writing PHP comments, you should clarify the purpose, logic and structure. 1. Each function and class uses DocBlock format to explain the role, parameters and return values; 2. Explain "why" in the key logic rather than just "what was done"; 3. Add a brief description at the top of the file, including functions, dependencies and usage scenarios; 4. Avoid nonsense comments, add only necessary instructions before complex logic, and do not record the modification history. This improves code readability and maintenance efficiency.

PHP Control Structures: If/Else PHP Control Structures: If/Else Jul 18, 2025 am 04:02 AM

When using if/else control structure for conditional judgment in PHP, the following points should be followed: 1. Use if/else when different code blocks need to be executed according to the conditions; 2. Execute if branches if the condition is true, enter else or elseif if they are false; 3. When multi-conditional judgment, elseif should be arranged in logical order, and the range should be placed in front of the front; 4. Avoid too deep nesting, it is recommended to consider switch or reconstruction above three layers; 5. Always use curly braces {} to improve readability; 6. Pay attention to Boolean conversion issues to prevent type misjudgment; 7. Use ternary operators to simplify the code in simple conditions; 8. Merge and repeat judgments to reduce redundancy; 9. Test boundary values to ensure the complete logic. Mastering these techniques can help improve code quality and stability.

Working with PHP Strings Working with PHP Strings Jul 18, 2025 am 04:10 AM

PHP string processing requires mastering core functions and scenarios. 1. Use dot numbers or .= for splicing, and recommend arrays for splicing large amounts of splicing; 2. Use strpos() to search, replace str_replace(), pay attention to case sensitivity and regular usage conditions; 3. Use substr() to intercept, and use sprintf() to format; 4. Use htmlspecialchars() to output HTML, and use parameterized query to database operations. Familiar with these function behaviors can deal with most development scenarios.

why am I getting undefined index in PHP why am I getting undefined index in PHP Jul 18, 2025 am 04:12 AM

The "undefinedindex" error appears because you try to access a key that does not exist in the array. To solve this problem, first, you need to confirm whether the array key exists. You can use isset() or array_key_exists() function to check; second, make sure the form data is submitted correctly, including verifying the existence of the request method and field; third, pay attention to the case sensitivity of the key names to avoid spelling errors; finally, when using hyperglobal arrays such as $_SESSION and $_COOKIE, you should also first check whether the key exists to avoid errors.

PHP Comments and Syntax PHP Comments and Syntax Jul 18, 2025 am 04:19 AM

There are two ways to correctly use PHP annotation: // or # for single-line comments, and /.../ for multi-line comments. PHP syntax requires attention to the fact that each statement ends with a semicolon, add $ before the variable name, and case sensitivity, use dots (.) for string splicing, and maintain good indentation to improve readability. The PHP tag specification is for use to avoid unnecessary gaps. Mastering these basic but key details can help improve code quality and collaboration efficiency.

A Simple Guide to PHP Setup A Simple Guide to PHP Setup Jul 18, 2025 am 04:25 AM

The key to setting up PHP is to clarify the installation method, configure php.ini, connect to the web server and enable necessary extensions. 1. Install PHP: Use apt for Linux, Homebrew for Mac, and XAMPP recommended for Windows; 2. Configure php.ini: Adjust error reports, upload restrictions, etc. and restart the server; 3. Use web server: Apache uses mod_php, Nginx uses PHP-FPM; 4. Install commonly used extensions: such as mysqli, json, mbstring, etc. to support full functions.

PHP Comments for Teams PHP Comments for Teams Jul 18, 2025 am 04:28 AM

The key to writing PHP comments is to explain "why" rather than "what to do", unify the team's annotation style, avoid duplicate code comments, and use TODO and FIXME tags reasonably. 1. Comments should focus on explaining the logical reasons behind the code, such as performance optimization, algorithm selection, etc.; 2. The team needs to unify the annotation specifications, such as //, single-line comments, function classes use docblock format, and include @author, @since and other tags; 3. Avoid meaningless annotations that only retell the content of the code, and should supplement the business meaning; 4. Use TODO and FIXME to mark to do things, and can cooperate with tool tracking to ensure that the annotations and code are updated synchronously and improve project maintenance.

PHP's Superglobal Variables PHP's Superglobal Variables Jul 18, 2025 am 04:28 AM

PHP has five most commonly used hyperglobal variables, namely $\_GET, $\_POST, $\_SERVER, $\_SESSION, and $\_COOKIE. ①$\_GET is used to obtain parameters in the URL, suitable for non-sensitive data transmission such as paging and filtering, but attention should be paid to input verification; ②$\_POST is used to receive sensitive data submitted by the form, such as login information, and it is necessary to prevent SQL injection and XSS attacks; ③$\_SERVER provides information about the server and script execution environment, such as the current script name, user IP and request method, and check whether the key exists before use; ④$\_SESSION is used to maintain user status across pages, and session\_st must be called first when using it.

See all articles