Using PHP and MySQL Web Development notes (2)

Source: Internet
Author: User

Chapter 5 code reuse and function writing


Require () or include () can load a file into a PHP script.

The two functions are almost equivalent. The only difference is that require () gives a fatal error after the call fails, while include () only gives a warning.

 

If the web page has a consistent appearance, you can separate the same part into a separate page. Then, you can use the require () Statement in PHP to add template elements to the page.

To ensure that a file is treated as plain text or HTML without executing any PHP code, you can use readfile () as an alternative.

 

Require_once () and include_once () can ensure that a file is only contained once. When using the function library, these two functions are very useful and can prevent accidental multiple times containing the same function library, this leads to repeated function definitions and errors.

 

You can also use the auto_prepend_file and auto_append_file options in the PHP. ini configuration file to set headers and footers so that they can be loaded before and after each page. In this case, the include () statement can no longer be used.

Auto_prepend_file = "/home/***/header. Inc"

If Apache is used, you can separately set each directory, create a file named. htaccess in the directory, and include the following code in the file:

Php_value auto_prepend_file "/home/***/header. Inc"

(Many other PHP options can also be set in this way)

 

Function declaration method:

Function my_function ()

{Echo 'myfunction ';}

Function calls are case insensitive. Function_name () is equivalent to function_name ().

 

Function definitions in PHP cannot be the same as existing functions, that is, function Overloading is not supported. But it can contain the default parameters.

PHP can also declare a function that can receive the number of variable parameters, through the following three functions:

Func_num_args (), func_get_arg (), and func_get_args ().

Function var_args ()

{

Echo "number of parameters :";

Echo func_num_args ();

$ ARGs = func_get_args ();

Foreach ($ ARGs as $ Arg)

Echo $ Arg. '<br/> ';

}

 

PHP has super global variables that are visible anywhere.

The use of require () and include () does not affect the scope. The contained global scope variables are still available.

The keyword "Global" can be used to manually specify that the variables defined or used in a function have a global scope.

Unset ($ var) can be used to manually delete a variable.

 

PHP function parameters also include "value transfer" and "Reference.

Function increment (& $ value, $ amount = 1 ){}

$ Value indicates reference transfer, and $ amount indicates value transfer.

 

Return can exit function execution. You can also use return to return a value from the function.

Function larger ($ X, $ Y ){

If ($ X >=$ y) return $ X;

Else return $ Y;

}

 

PHP supports recursive functions.

 

 

Chapter 6 object-oriented PHP

A basic PHP class:

Class classname

{

Function _ construct ($ PARAM) // Constructor

{

Echo "constructor called with parameter $ Param <br/> ";

}

Function _ destruct () // destructor

{

Echo "destruct ";

}

Function operation ($ ATT) {// common member function

$ This-> attribute = $ ATT;

}

VaR $ attribute; // common member variable

 

// You can use the _ Get and _ set functions to obtain the class attribute values.

Function _ Get ($ name) // The $ name parameter specifies the attribute name, which should be $ attribute

{

Return $ this-> $ name;

}

Function _ set ($ name, $ value) // $ value is the set value.

{

This-> $ name = $ value;

}

}

Use:

$ A = new classname ();

$ A-> $ attribute = 5; // This statement indirectly calls the _ set () function.

$ A-> $ attribute; // call the _ Get () function indirectly.

Through _ Get () and _ set (), you can check the data to be accessed to ensure that the data is meaningful.

 

The access modifier is also referenced in PhP5. You can use the private, public, and protected keywords to control access.

The default access modifier is public.

Class classname

{

Public $ attribute;

Public Function _ Get ($ name ){/**/}

}

 

Implement inheritance:

Class {

VaR $ attr1;

Function oper1 (){}

}

Class B extends {

VaR $ attr2;

Function oper2 (){}

}

B objects can also access $ attr1 and oper1 ();

In inheritance, you can use private and protected to control visibility.

If oper1 () of A is private, it cannot be accessed in B. Both protected and public can be used (same as C ++)

 

In PHP, "overload" is actually the coverage in C ++. The subclass function name is the same as the parent function name, and the same name function of the parent class is hidden.

The final keywords introduced in PhP5 can be inherited or overloaded.

Class {

Final function Opera (){}

}

The Opera () function cannot be overloaded in the subclass.

 

PHP does not support multiple inheritance, but supports interfaces.

Interface displayable

{

Function display ();

}

Class webpage implements displayable

{

Function display () {/* Implementation Code */}

}

 

Processing speed is very important for busy websites. We should try to use static html webpages or cache Script output as much as possible to reduce loading operations on servers.

 

In PhP4, objects are passed by value, but in PhP5 they are passed by reference.

Previous PHP versions cannot indirectly reference objects returned by functions, such as select ()-> display ();

PhP5 references the constant per-class, which can be used without class initialization.

Class math {

Const Pi = 3.14;

}

You can directly use math: pi.

PhP5 also introduces the static keyword, which allows class methods that can be called without class initialization. (This keyword cannot be used in static methods)

PhP5 provides the concept of instanceof keywords and type prompts.

PhP5 introduces the clone keyword, allowing you to copy an existing object. $ C = clone $ B;

Create the _ clone () method in the base class to customize the clone behavior without using the default one.

PhP5 also introduces "abstract classes" and "abstract methods ".

Any class containing abstract methods must be abstract.

Abstract class {

Abstract function operationx ($ PARAM );

}

 

You can overload the _ call () method.

Public Function _ call ($ method, $ P)

{

If ($ Method = 'display') {If (...) else if (...)}

}

You can call different functions based on the call implementation when calling the display method.

 

The _ autoload () method is a separate function that is used to automatically load the "class name. php" file when a class is not declared.

 

Generator and iteration

You can use the foreach () method to retrieve all attributes of an object in a loop, just like the array method. (Generated by the engine by default)

However, if more complex functions are required, only one iterator can be implemented.

 

The _ tostring () function can convert a class to a string. When the echo class is used, the corresponding string is displayed.

 

Reflection API:

$ Varcls = new reflectionclass ('page'); // page is a class name.

Echo $ varcls; // you can print the page class information.

 

 

Chapter 7 Exception Handling

In PHP, an exception must be thrown manually.

Throw exception class;

For example, you can throw a built-in exception class: Throw new exception ('message', Code );

Catch is used externally.

 

The exception class provides many built-in methods:

Getcode () -- return the Code passed to the constructor.

Getmessage () -- return the message passed to the constructor.

GetFile () -- returns the complete path of the code file that generates an exception.

Getline (), gettrace (), gettraceasstring (), _ tostring, etc ......

 

You can also throw any custom class object, but generally the custom exception class will be inherited from the exception.

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.