Some skills of personal exploration, everyone will write their own experience together.

zhaozj2021-02-16  56

Here, some individual experience is summarized, and you may have been using it. Written, the purpose is to throw bricks and jade, learn from each other, I hope everyone can summarize their own experience, set into a big post. The statements mentioned in the article are self-proclaimed. If there is nothing, welcome criticism.

1. Analog dynamic creation and inheritance, polymorphism, can be used to implement the interface uniform, and provide different underlying implementations, such as providing a document class to the upper layer, this document can be implemented to the database or concrete implementation to text I / O. It is also possible to combine 2 below, and create information hidden by objects. The inheritance of PHP is very familiar, derived inheritance of the function of the base class, when there is no existence in the called function, it will turn to call the function of the base class, or use baseName :: func () to skip the derived class. The function is directly called directly.

For the polymorphism (there is a gap with the real polymorphism, it is actually a similar characteristic of the similar polymorphism), because unlike C , PHP is weak type, cannot point to the base class pointer to derived To achieve, it is possible to introduce analog, first define a function X (), and the derived class is no longer redefined, and another function that needs to implement the polymorphism in this function is called. The following features.

If there is a definition y (), there is Y () in the derived class in the class class, Y () in the base class will be prioritized; if Y () is not defined in the base class, the base class is used as an abstract class, different Detective class defines different Y (), so that X () is called in different derived class instances, and similar polymorphism is achieved. A polymorphic feature is: all derived classes require a range of common operations (inter-function inter-function), so we naturally define this write operation to the base class, but one of the links Z () is required to Detecting classes can be implemented concrete, so we do not define Z () in the base class, and change it in the derived class.

It is worth mentioning that there is a dynamically created feature, when the new object or call function, in addition to the class name and function name can be written directly in the source code, the method of replacing the class name and function name can be used. Such as: $ class_name = "class1"; new $ class_name; // is equal to new class1; for the function call we mentioned in 3. ShowSelf ();} function ShowSelf () {echo " I'm base "."
";}} class type1 extends base {

Function showself () {echo "I'm type 1
";}} class type2 extends base {function showself () {echo "I'm type 2
"}}}} class type3 extends base {

} Class Type4 Extends Base {Echo "I'm Type 4, But I'll Call Base:"; Base :: Showself ();}} $ param_arr = "1234332131413412344132412"; Dynamic Creation Simulation Example For ($ I = 0; $ Param_arr [$ I]; $ I ) {$ TMP = "TYPE". $ param_arr [$ I]; $ instance [] = new $ tmp;}

//// Inherit, polymorphic simulation example for ($ I = 0; $ param_arr [$ i]; $ i ) {$ INSTANCE [$ I] -> show ();}?>

2. Convenient and fast to add custom hidden information PHP joining data members do not need to declare, use is definition, this feature is good and bad, do not discuss, we can use it flexibly, realize it to achieve it. Define information into an instance created or hide some custom operations.

?

Class info {var $ type; var $ name; function showinfo () {echo 'my type is:'. $ this-> type. ', my name is:'. $ this-> name. '
'; }}} class obj {var $ objs; function showallobjinfo () {for ($ I = 0; $ this-> objs [$ I]; $ i ) {$ tmp = $ this-> objs [$ I]; global $ $ TMP; Echo $$ TMP-> Info-> showinfo ();}}} $ obj = new obj; function newobj ($ = new $ type; $ info = new info; $ info-> type info; $ = type; = $ TYPE; $ info; return $ out;} Function Mynew ($ TYPE, $ NAME) {Global $$ Name, $ OBJ; $$ Name = Newobj ($ TYPE); $$ Name- > info-> name = $ name; $ obj-> objs [] = $ name;

/

Class derive1 {

}

Class derive2 {} /// mynew ("derive1", "instim"); Mynew ("Derive1", "INST2"); Mynew ("Derive2", "INST3"); $ INST1-> Info-> showinfo () ; $ Obj-> showallobjinfo ();?>

3. The PHP variable is not classified, and the PHP array can be conversion, and this feature is similar to the container. I don't know if it is setting a problem, my PHP5 is still a pass value when the parameter is passed (all parameter passed by the PHP5 is transmitted by default), and you need to add & the description is the pass reference. The characteristics used in this example are all mentioned in the use variable representation function name or class name, and the characteristics of similar function pointers are implemented, and the container characteristics can be used to implement a callback function and a generic algorithm. ?

//// Test section 1 5; Mysort ($ Arr, "SortProc"); Function Sortproc ($ Param1> $ Param2);} for ($ I = 0; $ Arr [$ I]; $ i ) { Echo $ ARR [$ I]. "
";

// Test section 2 $ arr [4] = "Abcde"; mysort ($ Arr, "Sortlength"); Function Sortlength ($ Param1, $ Param2) {Return (Strlen ($ Param1)> Strlen ($ Param2));} for ($ I = 0; $ 11; $ i ) {Echo $ Arr [$ I]. "
";}?> Press the example on the database query module: db_name = $ GLOBALS [ 'mysql_db_name']; $ this-> table_name = $ GLOBALS [ 'mysql_table_name']; $ this-> dbh = mysql_pconnect ($ Globals ['mysql_hostname ", $ globals [' mysql_username"], $ globals ['MySQL_Password']);

Function Query ($ query, $ call_back_function = "defproc", $ param = ") // uses a callback function, the third parameter defines the callback function name, for the global function, the original shape, see the following EXAMPLE, the fourth is transmitted Parameters, you can also use references to incoming {$ results = mysql_db_query ($ this-> db_name, $ query, $ this-> dbh); switch ($ results) {copy false: {$ call_back_function (false, $ param); Return false;} case 1: {$ call_back_function ($ result, $ param); return true;} default: {$ flag = false; while ($ row = mysql_fetch_array ($ result)) {$ call_back_function ($ row, $ param) $ Flag = true;} mysql_free_result ($ result); return $ flag;}}}} Function Defproc ($ ROW, $ Param) // Default handler {

} / Function DBPROC ($ ROW, $ Param) {Echo "this is from database:" $ row ['Field_Name']. "
"; Echo $ param;} $ connect = new hDatabase; $ connect-> Query ("Select * from Table_name", "DBProc", "this is an appended parameter");?> Unlimited-level directory and copy code Windows: Exec ("Xcopy32 / s SRC_Dir Di_Dir", $ STR, $ S VAR); Linux: EXEC ("cp -r ........ If you need complex interaction, you can use the pipe POPEN (); or more powerful proc_open (); // php4.3.04. For logic errors when debugging However, 2 in the 2 method can be used, and the error handling object is hidden inside the debug object, which can be distributed inside the object, which can significantly represent an error of the host. Because the global object $ Obj records the name of the instance, Global access to the error message of each object.

Class info {var $ type; var $ name; var $ error;

Function Adderror ($ code) {$ this-> error [] = $ code;}

Function showmyError () {Global $ Err_code; for ($ I = 0; $ this-> Error [$ I]; $ I ) Echo $ Err_code [$ this-> Error [$ I]]. "
"; }}} class obj {var $ objs; function showaller () {for ($ I = 0; $ this-> objs [$ I]; $ i ) {$ tmp = $ this-> objs [$ I]; global $ $ TMP; Echo $$ TMP-> Info-> ShowmyError ();}}} $ obj = new obj; function newobj ($) {$ out = new $ type; $ info = new info; $ = type = $ TYPE; $ info; return $ out;} Function Mynew ($ TYPE, $ NAME) {Global $$ Name, $ OBJ; $$ Name = Newobj ($ TYPE); $$ Name- > info-> name = $ name; $ obj-> objs [] = $ name;} Class Derive1 {function do1 () {$ i = 1; if ($ i == 1) $ this-> info-> adderror (1); Else $ this-> info-> adderror (2);}}

Class Derive2 {Function Do2 () {

$ I = 2; if ($ i == 1) $ this-> info-> adderror (1); Else $ this-> info-> adderror (2);

}} /// mynew ("derive1", "instim"); Mynew ("Derive1", "INST2"); Mynew ("Derive2", "Inst3"); $ INST1-> DO1 (); $ INST3-> DO2 (); echo "The following lines list error in instip1:
"; $ INST1-> Info-> showmyError (); echo "The following lists all error:
"; $ obj-> showallerror () ;?> 5. MCV

转载请注明原文地址:https://www.9cbs.com/read-20122.html

New Post(0)