PHP Tutorial Function-system function recursive function reuse function constructor use
/*
* 1. Intrinsic functions: PHP can declare functions inside a function
* The purpose is to call inside the function
* Used to help external functions complete some sub-functions
*
* 2. Recursive function: Call your own function name within yourself
*
* 3. Reusing functions
*
* Require: for static inclusion
* Include: for dynamic inclusion
* Require_once: For static inclusion, including only once
* Include_once: For dynamic inclusion, including only once
*
* 4. Use of some system functions
* Resource =opendir ("directory Name")
* READDIR (Resources)
*
*
*/
Intrinsic functions
Function score ($php, $java, $dotnet)
{
function php ($php)
{
if ($php >60)
return "Pass";
Else
Return "Failed";
}
function Java ($java)
{
if ($java >60)
return "Pass";
Else
Return "Failed";
}
function dotnet ($dotnet)
{
if ($dotnet >60)
return "Pass";
Else
Return "Failed";
}
$total = $php + $java + $dotnet;
$AGV = $total/3;
echo "Your PHP score is {$php} points,". PHP ($php). "
";
echo "Your Java score is {$java} points,". 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 functions
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 directory:". $dirnum. "
";
echo "Total Files:". $filenum. "
";
?>
Constructors and destructors
constructor function
void __construct ([Mixed $args [, $ ...])
PHP 5 allows the developer to define a method as a constructor in a class. Classes with constructors Call this method each time an object is created, making it ideal for doing some initialization work before using the object.
Note: If a constructor is defined in a subclass, the constructor for its parent class is not called secretly. To execute the constructor of the parent class, you need to call Parent::__construct () in the constructor of the child class.
Example #1 using 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 ();
?>
For backwards compatibility, if PHP 5 cannot find the __construct () function in the class, it tries to find the old-fashioned constructor, which is the function with the same name as the class. So the only thing that can create a compatibility problem is that there is already a method named __construct () in the class, but it is not a constructor.
Destructors
void __destruct (void)
PHP 5 introduces the concept of destructors, similar to other object-oriented languages such as C + +. Destructors are removed when all references to an object are deleted or executed when an 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 ();
?>
As with constructors, the destructor of the parent class is not called by the engine. To execute a destructor for a parent class, you must explicitly call Parent::__destruct () in the destructor body of the subclass.
Note:
The destructor is called when the script is closed, at which point all header information is sent.
Note:
Attempting to throw an exception in a destructor can result in a fatal error.
Class Foobar {
Public $baz;
function __destruct () {
# Don ' t do either of these, if $baz also have a __destruct ()!
$this->baz = null;
unset ($this->baz);
# Instead, don ' t clear it at all, or does this:
$this->baz->__destruct ();
}
}
?>
If you made the mistake, this might happen in php<5.3.6:
# Some function that throws an exception
function fail ($foobar) {
throw new Exception ("Exception a!");
}
$foobar = new Foobar ();
$foobar->baz = new Foobar ();
try {
Fail ($foobar); Send Foobar to Func-throws an Exception
} catch (Exception $e) {
Print $e->getmessage (); Exception A would be caught and printed, as expected.
}
$foobar = null; Clearing Foobar, and its property $baz
try {
print ' Exception B: ';//This'll be printed
The output stops tutorial here.
throw new Exception ("Exception b!");
} catch (Exception $e) {
Print $e->getmessage (); doesn ' t happen
}
print ' End '; This won ' t is printed
?>
http://www.bkjia.com/PHPjc/445341.html www.bkjia.com true http://www.bkjia.com/PHPjc/445341.html techarticle PHP Tutorial Function-system function recursive function reuse function constructor use/* * 1. Intrinsic functions: PHP can declare functions inside functions * to call inside a function to help ...