Home PHP Libraries Other libraries PHP constant operation library
PHP constant operation library
<?php
class Foo
{
  const BAR = 'bar';
  public static function getConstantValue()
  {
    return self::BAR;
  }
  public function getConstant()
  {
    return self::BAR;
  }
}
$foo = 'Foo';
echo $foo::BAR, '<br />';
echo Foo::BAR, '<br />';
$obj = new Foo();
echo $obj->getConstant(), '<br />';
echo $obj->getConstantValue(), '<br />';
echo Foo::getConstantValue();
class Bar extends Foo
{
  const BAR = 'foo'; 
  public static function getMyConstant()
  {
    return self::BAR;
  }
  public static function getParentConstant()
  {
    return parent::BAR;
  }
}
echo Bar::getMyConstant(); // foo
echo Bar::getParentConstant(); // bar

Class constants belong to the class itself, not to object instances, and cannot be accessed through object instances

Cannot be modified with public, protected, private, static

Subclasses can override parent classes The constants in the parent class can be called through (parent::)

Since PHP5.3.0, a variable can be used to dynamically call the class. But the value of this variable cannot be the keyword


Disclaimer

All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn

Related Article

File operation library in PHP8.0 File operation library in PHP8.0

14 May 2023

PHP8.0 is the latest version of the PHP programming language, which provides many new features and improved libraries. Among them, the file operation library has also been greatly improved and expanded, providing developers with a more efficient and flexible file operation method. File operations are an integral part of web development, and almost every web application needs to read, write, and process files. Sometimes, developers need to save uploaded files to the server, and sometimes they need to read configuration files or other resource files on the server. PHP8.

GD library operation guide in PHP GD library operation guide in PHP

20 May 2023

1. What is the GD library? The GD library is a set of library functions for creating and processing various image formats. It is one of the most commonly used image processing libraries in PHP. 2. Install the GD library Install the GD library under CentOS/RedHat 1. Install PHP’s GD extension library yuminstallphp-gd 2. Restart the web server servicehttpdrestart 3. Check the GD library version supported by PHP php-i | grep-igd in Ubunt

How to encapsulate PHP database operation function library How to encapsulate PHP database operation function library

03 Apr 2023

With the rapid development of the Internet, databases have become an important part of many websites and applications. Database operations are also one of the basic operations of websites and applications. With the development of PHP language, PHP has become the development language for most websites and applications. The MySQL extension library and its encapsulation functions provided by PHP enable PHP to easily interact with the MySQL database and realize the addition, deletion, modification and query operations of the database. In actual development, function libraries that encapsulate database operations can greatly improve development efficiency and code readability. In this article, we

Looking for a php/python library management program (similar to Baidu library, managing doc/pdf and other libraries) Looking for a php/python library management program (similar to Baidu library, managing doc/pdf and other libraries)

30 Sep 2016

Looking for a php/python library management program (similar to Baidu library, managing doc/pdf and other libraries)~~ It mainly needs to have search functions, especially file classification retrieval/file tag retrieval functions, no need for online conversion, online browsing!

XML operation class implemented by PHP [XML Library] XML operation class implemented by PHP [XML Library]

06 Jan 2017

This article mainly introduces the XML operation class implemented by PHP, involving PHP's conversion, serialization, deserialization and other related operation skills for arrays and xml. Friends in need can refer to the following

Integration of PHP function library and third-party library Integration of PHP function library and third-party library

22 Apr 2024

Function libraries and third-party libraries in PHP can extend the functionality of applications. The function library provides predefined functions that can be included through the include statement. Third-party libraries are available from sources such as Packagist, GitHub, and installed using Composer. Implement automatic loading of classes through autoloaders, such as automatic loading of the Guzzle library. Learn how to use the Dompdf third-party library to generate PDF files through practical cases, including loading the library, loading HTML content, and outputting PDF files. The integration of function libraries and third-party libraries greatly expands the functionality of PHP applications and improves development efficiency and project performance.

See all articles