Carefully learn PHP object-oriented-2

Source: Internet
Author: User
Tags learn php stdin
learn PHP Object-oriented -2 carefully Preface

Ready to write a serious study of the PHP object-oriented series, using PHP to do the Web page, do not have a deep understanding of PHP, may be Sanbang enough, do not need to have much advanced. If there is any mistake, you are welcome to enlighten me. The progress of the arrangement, I learned where to update to where. The form of words on the use of a small case of demand, and then realize, and attach their own summary, article source of the use of the environment

System: ubuntu16.04
Editor: phpstorm2017 requirements: 1 parameter fault tolerance and automated invocation method resolution: __callstatic use and construct call function __ Callstatic triggers the execution of this function when calling a static function that does not exist for a class, we can use this to make a friendly error when entering a nonexistent parameter. Automatic invocation, when the parameters are not many, we can specify the function name in the class, but when the function is too much, this call is obviously very laborious, we can take the constructor name of the method to call the function implementation: Automated call GOD.HP

   Require ("god_calss.php");
    $result = ';
    if ($ARGC >=2) {
        $p = $argv [1];
        if (substr ($p, 0,1) = = '-') {
            $p =substr ($argv [1],1);
            $result =GOD_CALSS:: $p ();
        } else {
            $result = "Shuold add-to call the function!";
        }        '-V ' = = $argv [1] && $result =god_calss::version ();        '-init ' = = $argv [1] && god_calss::init ();        '-make ' = = $argv [1] && god_calss::make ();
    }
    echo $result;
    Echo Php_eol;
fault-tolerant processing god_class.hp
  static function __callstatic ($name, $arguments)
       {
           //Todo:implement __callstatic () method.
           echo "The function you called is undefined!";
       }
Effect:

requirements: 2) using StdClass temporary storage RETROFIT init Resolution: StdClass StdClass automatically loads this function when the PHP kernel is initializing module initialization, all of its magic methods, parent classes, interfaces, and so on, are set to NULL when initialized. Because we cannot dynamically add methods to a class in PHP, this class can only be used to handle dynamic property implementations: transforming init god_class.hp

static function init () {

       echo ' input your project_name? '. Php_eol;
       $PRJ _name=fgets (STDIN);

       echo  "Input your author_name?". Php_eol;
       $author =fgets (STDIN);

       echo "Your input:". Php_eol;

       Echo Json_encode (TC (' prj_name ' => $prj _name, ' author ' => $author));
   }

# #GOD_FUNC.HP
Returns an object that stores user input

function TC ($p) {
  $get _class=new stdClass ();
  foreach ($p as $k => $v) {
      $get _class-> $k = $v;
  }
  return $get _class;
}
god.hp

Be sure to precede the class file
Require ("god_func.php");
Require ("god_calss.php"); Effect:

Requirements: 3 Accept user Input Parameters Create folder Resolution: mkdir and File_get_contents and Json_decode

The specific usage may check descend NET Handbook
1. Before we accept user input and put it into the JSON file, now to do is read data from the JSON file, return the object, create the user input corresponding folder implementation: transform init god_class.hp

      static function Start () {
           $config =loadconfig ();
           Create Folder
           !file_exists (GETCWD (). ' /'. $config->prj_name) && mkdir (GETCWD (). ' /'. $config->prj_name);
           Create File
           !file_exists (GETCWD (). ' /'. $config->prj_name. ' /index.php ') && file_put_contents (GETCWD (). ' /'. $config->prj_name. ' /index.php ', ');
          

# #GOD_FUNC.HP

  function Loadconfig () {
        $file =file_get_contents (GETCWD ()). ' /god.json ');
        Return Json_decode ($file);
    
Effect:

Requirements: 4 automatically introduce the user needs to call the class file Resolution: __autoload and Namespace namespace use

The creation of the above file to customize a class, in order to build a more planned framework for the entire project, the project file directory becomes this way, the core represents the key file directory of the project, frame is our reference to the External class library file directory, template is the template file directory
implementation: Transform Init god_class.hp

Write the following code outside the class name

    Use Core\frame\god_frame;
    function __autoload ($className) {
        $className =str_replace (' \ \ ', '/', $className). ' PHP ';
            Require ($className);
    

Method call:

     static function Start () {
               $config =loadconfig ();
                $ff =new god_frame ($config->prj_name);
                $ff->run ();
           }

# #GOD_FRAME.HP

namespace Core\frame;
Class God_frame
{public
    $project _folder= "";//project portal file public
    $project _main= "";//Entry file
    function __ Construct ($prjName)
    {
        $this->project_folder=getcwd (). " /". $prjName;
        $this->project_main= $this->project_folder. ' /index.php ';
    }
    function run () {
        !file_exists ($this->project_folder) && mkdir ($this->project_folder);
        ! File_exists ($this->project_main) && file_put_contents ($this->project_main, ");
    }

Effect:

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.