Php function-system function recursive function reuse function constructor usage _ PHP Tutorial

Source: Internet
Author: User
Php functions-use of system function recursive functions to reuse function constructors. Php Tutorial function-system function recursive function reuse function constructor using ** 1. internal Function: PHP can declare a function within the function * The purpose is to call a function within the function * to help php Tutorial function-system function recursion function reuse function constructor usage

/*
* 1. internal function: PHP can declare the function inside the function.
* The purpose is to call the function internally.
* Used to help external functions complete some sub-functions
*
* 2. recursive functions: Call your own function name within yourself.
*
* 3. reuse functions
*
* Require: used for static inclusion
* Include: used for dynamic inclusion
* Require_once: used for static inclusion only once
* Include_once: used for dynamic inclusion, only once
*
* 4. use of some system functions
* Resource = opendir ("directory name ")
* Readdir (resource)
*
*
*/
// Internal functions
Function score ($ php, $ java, $ dotnet)
{
Function php ($ php)
{
If ($ php> 60)
Return "pass ";
Else
Return "fail ";
}
Function java ($ java)
{
If ($ java> 60)
Return "pass ";
Else
Return "fail ";
}
Function dotnet ($ dotnet)
{
If ($ dotnet> 60)
Return "pass ";
Else
Return "fail ";
}

$ Total = $ php + $ java + $ dotnet;
$ Agv = $ total/3;
Echo "your php score is {$ php},". php ($ php )."
";
Echo "your java score is {$ java},". java ($ java )."
";
Echo "your dotnet score is {$ dotnet},". dotnet ($ dotnet )."
";
Echo "your total score is: {$ total}
";
Echo "your average score is: {$ agv}
";
}
Score (50, 90, 70 );

// Recursive function
Function demo ($ num)
{
Echo $ num ."
";
If ($ num> 0)
Demo ($ num-1 );
Else
Echo "--------------------------------
";
Echo $ num ."
";
}
Demo (10 );

Function total ($ dirname, & $ dirnum, & $ filename)
{
$ Dir = opendir ($ dirname );
Readdir ($ dir )."
";
Readdir ($ dir )."
";
While ($ filename = readdir ($ dir ))
{
$ Newfile = $ dirname. "/". $ filename;
Echo $ filename ."
";
If (is_dir ($ filename
}

}
$ Dirnum = 0;
$ Filenum = 0;
Total ("c:/windows", $ dirnum, $ filenum );
Echo "total number of directories:". $ dirnum ."
";
Echo "total number of files:". $ filenum ."
";
?>

Constructor and Destructor
Constructor
Void _ construct ([mixed $ args [, $...])
PHP 5 allows developers to define a method in a class as a constructor. Classes with constructors call this method each time an object is created, so it is very suitable for initialization before using the object.

Note: If the constructor is defined in the subclass, the constructor of its parent class will not be called secretly. To execute the constructor of the parent class, you must call parent ::__ construct () in the constructor of the subclass ().

Example #1 use the new standard constructor

Class BaseClass {
Function _ construct (){
Print "In BaseClass constructorn ";
}
}

Class SubClass extends BaseClass {
Function _ construct (){
Parent: :__ construct ();
Print "In SubClass constructorn ";
}
}

$ Obj = new BaseClass ();
$ Obj = new SubClass ();
?>


To achieve backward compatibility, if PHP 5 cannot find the _ construct () function in the class, it will try to find the old constructor, that is, the function with the same name as the class. Therefore, the only situation that causes compatibility problems is that the class already has a method named _ construct (), but it is not a constructor.

Destructor
Void _ destruct (void)
PHP 5 introduces the concept of destructor, which is similar to other object-oriented languages, such as C ++. The Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed.

Example #2 destructor Example

Class MyDestructableClass {
Function _ construct (){
Print "In constructorn ";
$ This-> name = "MyDestructableClass ";
}

Function _ destruct (){
Print "Destroying". $ this-> name. "n ";
}
}

$ Obj = new MyDestructableClass ();
?>


Like constructors, the parent class's destructor will not be called by the engine. To execute the destructor of the parent class, you must explicitly call parent ::__ destruct () in the destructor of the subclass ().

Note:

The Destructor is called when the script is disabled. all header information is sent.


Note:

Throwing an exception in the destructor will cause a fatal error.

Class Foobar {
Public $ baz;
Function _ destruct (){
# Don't do either of these, if $ baz also has a _ destruct ()!
$ This-> baz = null;
Unset ($ this-> baz );

# Instead, don't clear it at all, or do this:
$ This-> baz->__ destruct ();
}
}
?>

If you made this mistake, this might happen in php <5.3.6:
# Some function that throws an exception
Function fail ($ foobar ){
Throw new Exception ("Exception! ");
}

$ Foobar = new Foobar ();
$ Foobar-> baz = new Foobar ();

Try {
Fail ($ foobar); // Send foobar to func that throws an Exception
} Catch (Exception $ e ){
Print $ e-> getMessage (); // Exception A will be caught and printed, as expected.
}

$ Foobar = null; // clearing foobar, and its property $ baz

Try {
Print 'exception B: '; // this will be printed
// Output stops tutorial here.
Throw new Exception ("Exception B! ");
} Catch (Exception $ e ){
Print $ e-> getMessage (); // doesn' t happen
}
Print 'end'; // this won't be printed
?>


Reuse recursive function reuse function constructor use/** 1. internal function: PHP can declare a function within the function * The purpose is to call the function * to help...

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.