Dr. Shen Yi's notes on PHP devil training (4), Shen Yi's devil

Source: Internet
Author: User

Dr. Shen Yi's notes on PHP devil training (4), Shen Yi's devil

In the previous lesson, we copied code through shell scripts and learned about static methods and static attributes. (There is also an attachment PHAR package, which I directly ignore)

Then, some parameters are written in the GOD file,

We can also combine the method name like a string.

 $get_param=$argv[1]; godinit::$get_param();

Well, next we will follow the instructor's course to implement the requirement: If the parameter contains "-", the description is attribute. Directly call static attributes of a class. If "-" is not included, it indicates the method. We call the static method of the class directly.

So I first changed the god file written in the previous lesson to the following:

#! /Usr/local/bin/php <? Phprequire ('God _ func7'); require ("godinit. php "); $ result =''; if ($ argc> = 2) {/* '-V' = $ argv [1] & $ result = godinit :: $ V; 'make' ==$ argv [1] & $ result = godinit: make (); 'init' = $ argv [1] & $ result = godinit: init (); */$ p = $ argv [1]; // obtain the parameter if (substr ($ p,) = '-') // indicates that the variable is obtained and matched. {// if the passed parameter is-v, it will become v $ p = substr ($ p, 1); $ result = godinit: $ p;} else {$ result = godinit :: $ p () ;}} echo $ result; echo PHP_EOL;?>

The result is as follows:

  

  

We return to the command line above. If we output a non-v character at will, an error will occur, so we need to complete the code.

$ P = $ argv [1]; // obtain the parameter if (substr ($ p,) = '-') // to obtain and match the variable, {// If-v is passed in, it will become v $ p = substr ($ p, 1); $ result = isset (godinit: $ p )? Godinit: $ p: error;} else {$ result = godinit: $ p ();}

Here we will use a PHP magic function _ callStatic ($ m, $ args ). If you want to use this method, it must be written to the class and static. This function is automatically triggered if you call an undefined static method. The first parameter is the method name, and the second parameter is the method parameter. We can use it for fault tolerance Processing (as long as we learn this point). In the godinit file, we add this method:

static function __callStatic($p1,$p2){    echo '$p1';}

Then, let's look at the results:

  

Now let's take a look at the general class definition, instantiation, and calling. Create a godconfig file and create a godconfig class with only attributes.

<?phpclass godconfig{    public $prj_name;    public $prj_author;    }?>

In godinit, the instantiation class godconfig uses 1. json_encode (the class after instantiation): returns a json string 2. json_decode (json string): returns an object.

<? Phprequire ('godconfig. php '); // introduce the class godinit file gonconfig // create a class, godinit {static $ v = "god version is 1.2 "; // declare a static attribute $ VERSION static function init () // static method init {$ gc = new godconfig (); // instantiate echo "input your project name? ". PHP_EOL; $ gc-> prj_name = fgets (STDIN); // get the character entered by the user from the standard input and assign the value to the instantiation attribute $ prj_name echo" input your author name? ". PHP_EOL; $ gc-> prj_author = fgets (STDIN); $ ret = array (); // Initialize an array; $ ret [] = $ gc; echo json_encode ($ ret); // return ""} static function _ callStatic ($ p1, $ p2) {echo "error function" ;}}?>

Then let's look at the results:

  

 

 

A few simple knowledge points:

()? ():();

Substr ();

Isset ();

_ CallStatic ($ m, $ args)

Json_encode ()

Json_decode ()

 

Copyright statement: the note organizer loves freedom and advocates sharing. However, this note comes from "PHP devil training first stage" by teacher Shen Yi at www.jtthink.com. This learning note was first launched in the blog Park. If you need to repost it, please respect the work of the teacher and keep the signature of the instructor Shen Yi and the course source address.

 

Previous lesson: Shen Yi's PHP devil training notes (3)

 

Next lesson:

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.