PHP functions:
The 1:php function does not support overloading, and the function is case-insensitive, but it is recommended that case-sensitive ~~! need harmony after all!
2: Recursive functions do not have too many recursion, and may cause the script to crash because of a stack overflow.
<?php
function reverse ($a) {
if ($a < 20) {
echo "This is $a";
$a + = 1;
Reverse ($a);
}
}
3: Parameters are passed by value by default, scalar. If you want to pass by reference, add &.
Default parameters:
function Default_param ($a = "Default value") {
echo "$a";
Return
}
Null and empty are not the same.
Default_param (); Default value;
Default_param (NULL); // ;
Default_param ("Hahha"); Hahha
Something else. Non-scalar value:
function Makecoffee ($type = Array ("Test")) {
}
3:
Parameters that pass non-default values are placed to the left of the default parameter.
This is the wrong demonstration:
<?php
function Makeyogurt ($type = "Acidophilus", $flavour)
The right demonstration:
<?php
function Makeyogurt ($flavour, $type = "acidophilus");
4: Multiple parameters are passed, which I understand as dynamic parameters:
<?php
function sum (... $number) {
$ACC = 0;
foreach ($number as $n) {
$ACC + = $n;
}
Echo $ACC;
}
SUM (1,2,3,4);
Parameters can define types
function sum (datainterval ... $interval) {
Do ...
}
In other words, the incoming interval is the requirement is datainterval.
5:return return
When there is no return, the default is null.
If you want to return more than one value, you can implement a similar effect through an array.
If you want to return a reference, you need to handle it in two places
<?php
function &reference_func () {
return $some _ref; This is the official wording. I don't think it's clear.
$val = "value";
return $val;
}
Use this return value
$user _ref = & Reference_func ();
Variable functions:
This is excellent.
<?php
function foo () {
echo "This is function foo\n";
}
function Bar () {
echo "This is function bar\n";
}
$func = ' Foo ';
$func ();
$func = ' Bar ';
$func ();
6: Anonymous function: Also called closed bag ~!
<?php
$greet = function ($name) {
printf ("Hello%s \ r \ n", $name);
}
$greet ("Ni Hao");
Basic use, as well as the scope of the variables. Look again later.
7:class's introduction.
New section, I feel super excellent!!
For example, new classes can play this way.
$classname = ' Foo ';
$instance = new $classname (); Foo ()
It feels like as long as you build the class, you can load it dynamically, and the code will be very concise.
The inheritance of the class. Cannot inherit more, a class can inherit only one class
Ways to access properties: It's really a mix of Python and C + + languages.
Accessing non-static properties: $this->property
Access static properties: Self:: $property
Constants in the class are used:
Class myclass{
CONST CONSTANT = ' constant value ';
function Showconstant () {
Echo Self::constant. "\ n";
}
}
Accessing the const outside of the class
$class = new MyClass ();
echo $class:: Constant. "\ n" ";
Automatically loads the class. This is mainly used in referencing the definition class of the file, just like each Java class is a file, using the import, and then a bunch of import
<?php
function __autoload ($class _name) {
Require_once $class _name. PHP ';
}
$obj 1 = new MyClass1 ();
$obj 2 = new MyClass2 ();
__autoload will be abandoned later on.
So use it first.
7: Use of constructs and destructors:
__construct () {
}
__destruct () {
}
If you want to invoke the constructor of the parent class
__construct () {
Parent::__construct ();
do other construct;
}
:: This is called the range resolution operator. Constants that are typically used to access static constants, classes.
PHP Learning record "seven"