PHP Constants
PHP constant capitalization
PHP constants are defined with the Define function or the const keyword
Once a constant is defined, it can no longer be changed or Undefined.
After PHP 5.3.0, You can use the Const keyword to define constants outside of the class Definition.
Pre-defined constants:
__line__
__file__
Define("FOO1", "something1");Define("FOO2", "something2");Define("FOO3", "something3");Const CONSTANT= ' FOO ';EchoFOO1;Echo' <br/> ';EchoFOO2;Echo' <br/> ';EchoFOO3;Echo' <br/> ';Echo CONSTANT;
PHP5 Magic constant
__line__, the current line number in the file
__file__, the full path and filename of the file
__dir__, the directory where the files are located
__function__, function name
__class__, the name of the class
__trait__,trait's Name.
__method__, method Name of the class
__namespace__, the name of the current namespace
Header(' content-type:text/html; Charset=utf-8 ');Echo' Magic Constants ';Echo' <br/> ';Echo' Current line number: '.__line__;Echo' <br/> ';Echo' The full path and filename of the file: '.__file__;Echo' <br/> ';Echo' Directory where the file is Located: '.__dir__;Echo' <br/> ';functionmy_test () {Echo' The name of the current function: '.__function__;} My_test ();Echo' <br/> ';classMy_class { public $name=__class__;}$my _class=NewMy_class ();Echo' The name of the current class: '.$my _class-name;Echo' <br/> ';
PHP operators
PHP Control statements
$a = 5; $b = 3; if ($a$b) Echo "a is bigger than b";
$people=Array( Array(' name ' = ' Namea ', ' salt ' = 1111),Array(' name ' = ' nameb ', ' salt ' = 2222)); for($i= 0,$size=sizeof($people);$i<$size;$i++) { $people[$i[' Salt '] =Rand(0000, 9999);}Var_dump($people);
$arr=Array(1, 2, 3, 4);foreach($arr as&$value) {//you must write a reference, or you will get an error executing the code block BELOW. $value=$value* 2; Echo $value.‘ <br/> ';}Var_dump($arr);Echo' <br/> ';unset($value);//Cancel the referenceVar_dump($arr);
Break statement
Break ends the execution of the current for,foreach,while,do-while or switch structure. Break can accept an optional numeric parameter to decide to jump out of a few loops.
Break can accept parameters, stop execution of several structures, default is 1
$arr=Array(' one ', ' one ', ' three ', ' four ', ' Stop ', ' five '); while(List(,$var) = each($arr)) { if($var= = ' Stop ') { break; } Echo"$var<br/> ";}$i= 0; while(++$i) { Switch($i) { case5:Echo' At 5 <br/> '; break1;//Exit switch only case10:Echo' At Ten <br/> '; break2;//Exit Switch and while default: break; }}
Continue accepts an optional numeric parameter to decide to skip a few loops to the end of the Loop. The default value is 1
DECLARE statements
What is a low-level statement? It includes:
Simple statement: Empty statement (on one; number), return, break, continue, throw, goto, global, static, unset, echo, built-in HTML text, semicolon-terminated expression, etc. all count as one Statement.
Compound statement: The complete if/elseif, while, do...while, for, foreach, switch, try...catch, etc. are counted as a statement.
Statement Block: a block of statements enclosed in {}.
Finally special: the declare block itself is counted as a statement (according to the truth declare block is also a compound statement, but here deliberately separate it out).
Declare(ticks=1);//Low level Statement//A function called on each tick eventfunctionTick_handler () {Echo"tick_handler () called<br/>";//Low-level Statements}register_tick_function(' Tick_handler ');//Low-level Statements$a= 1;//Low-level Statementsif($a> 0) { $a+ = 2;//Low-level Statements Print($a);//Low-level Statements}
function do_tick () { echo "<b style= ' color:red; ' >do_tick_function</b><br> ";} register_tick_function (' Do_tick '); Declare (ticks = 1) { for ($i$i$i+ +) { Echo "<b style= ' color:blue; ' >{$i}</b><br> "; }}
The include statement contains and runs the specified file. Not found will only issue a warning.
Require didn't find a fatal mistake.
vars.php
$color = ' green '; $fruit = ' Apple ';
test.php
Echo $color $fruit"; include ' vars.php '; Echo $color $fruit";
PHP functions
(and The JS function basically identical)
Classes and objects
classA {functionfoo () {if(isset($this)) { Echo' $this is defined '.Get_class($this); //Get_class () Returns the class name}Else { Echo' $this is not defined '; } }}$a=NewA ();$aFoo ();
Inherited
PHP does not support multiple inheritance, and a class can inherit only one base class.
Inherited methods and members can be overwritten by a re-declaration with the same name, unless the parent class defines the method with the final Keyword. The overridden method or member can be accessed through the Parent::.
classSimpleclass {//member Declaration public $var= ' A default value '; //Method Declaration public functionDisplayvar () {Echo $this-var; Echo' <br/> '; }}$simple _class=NewSimpleclass ();$simple _class-Displayvar ();classExtendclassextendsSimpleclass {//overriding the parent class method functionDisplayvar () {Echo' Extend Class: '; Parent::d Isplayvar ();//Todo:change the autogenerated stub }}$extended=NewExtendclass ();$extendedDisplayvar ();
Property Modifier: Public,protected,private
Class constants
//class ConstantsclassMyClass {Const CONSTANT= ' constant value '; //after PHP5.3.0 ConstBar = <<< ' EOT 'bareot; public functionshowconstant () {Echoself::CONSTANT.‘ <br/> '; }}$my _class=NewMyClass ();$my _class-showconstant ();//direct access to class constantsEchoMyClass::CONSTANT;Echo' <br/> ';//after PHP5.3.0Echo $my _class::CONSTANT;Echo' <br/> ';Echomyclass::bar;
Auto Load Class
Many developers write object-oriented applications, Creating a PHP source file for each class Definition. A very big
Annoyance is having to write a long list of included files at the beginning of each script (one file per class).
In a software development system, it is not possible to write all the classes in a PHP file, when in a PHP file
When you need to invoke a class declared in another file, you need to introduce the file through Include. But sometimes,
In a large number of projects, to each of the required classes of files included in, is a very frustrating thing, so
Can we use any class to import the PHP file where this class is located? This is where we're
The Auto-load class to Speak.
In PHP5, you can define a __autoload () function that will automatically adjust when you try to use a class that is not already defined
By calling this function, the scripting engine has the last chance to load the required classes before PHP Fails.
The __autoload () function receives a parameter that is the class name of the class that you want to load, so when you do the project, the organization defines
The name of the class, you need to follow certain rules, preferably with the class name as the center, or you can add a uniform prefix or suffix shape
into filenames, such as xxx_classname.php, classname_xxx.php, and classname.php, and so On.
This example attempts to load the MyClass1 and MyClass2 classes from the myclass1.php and myclass2.php files, respectively
myclass1.php
class MyClass1 { publicfunction className () { echoget_ Class($this); }}
myclass2.php
class MyClass2 { publicfunction className () { Echo get_class($this); }}
demo01.php
// Auto Load Class function __autoload ($class _name) { require_once$class _name . '. php ';} $obj New MyClass1 (); $obj 2 New MyClass2 (); $obj-className (); Echo ' <br/> '; $obj 2->classname ();
PHP Classes and object-oriented