Constant Const property, static property, static function method in PHP

Source: Internet
Author: User

<?php
Constants in PHP
Define (' MYNUM ', 2000);
echo MYNUM. ' <br> ';
if (!defined (' MYNUM ')) {
Define (' MYNUM ', 4000);
}
Echo MYNUM;
In-class declaration and use
Class Testconst {
Const COUNTRY = ' China '; Const cannot be public, static does not need $, variable name is uppercase
static $static = ' static ';
Public Function Getcountry ()
{
Echo ' accesses the const attribute inside the class: '. Self::country. ' <br> '; In-class calls must be in the form of self (self+ range resolution operator + static variable name)
Echo ' accesses the static property inside the class: '. Self:: $static. ' <br> '; In-class calls must be in the form of self (self+ range resolution operator + static variable name)
Echo ' accesses the static function method inside the class: '. Self::test2 (). ' <br> '; Call static function method inside class self::+ static resource Name = = = This is the standard format
}
public static function Test2 ()
{
Echo '-I am a static method of function-';
}
}
$test = new Testconst ();
Var_dump ($test);
echo $test->getcountry (). ' <br> ';

Echo ' accesses the const attribute on the outside of the class: '. Testconst::country. ' <br> '; Const can be called directly using (the name of the class + range resolution operator + static variable name)
Echo ' External access to static properties of class: '. Testconst:: $static. ' <br> '; Static can be called directly using (the name of the class + range resolution operator + static variable name)
Echo ' accesses the static function method inside the class: '. Testconst::test2 (). ' <br> '; Call static function method self::+ static resource name in class
?>
<!--in-script declaration and use-
<?php
Const COUNTRY = ' Japan ';
Echo Country. ' <br> ';
?>

Constant Const property, static property, static function method in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.