IOS programmers learn how to quickly master PHP, transform & quot; full stack Siege & quot ;?

Source: Internet
Author: User
Tags php editor
IOS programmer: how to quickly master PHP, Avatar & amp; quot; full stack siege Lion & amp; quot ;?

This is a PHP Getting Started Guide written by iOS developers to iOS programmers. in this article, I am trying to explore the commonalities between objectiv-c and php to help the attacker with certain iOS development experience to quickly get started with a background development language. the background development language is the one that appears in our development document in the form of "data interface! Mastering PHP will be of great benefit to your current ios development work and the long-term development of your career in the future! Most importantly, PHP itself is not a toy language, but a considerable number of companies are still using background development languages, including your current company. this article, it is not a simple basic manual, but a more suitable way for iOS developers to systematically explain the most important and common concepts and functions in PHP. reading and effectively practicing this article will help you to write the background data interface independently.

Necessary preparations and instructions

First, you need to download the latest XAMPP software to build a php server locally.: https://www.apachefriends.org/download.html.

After the download is complete, double-click to install. after the installation is successful, choose Mange Servers --> Start All to Start the local server. after successful startup, enter http: // localhost in the browser to view a default PHP page.

By default, your php server files are stored in: application --> XAMPP --> htdocs directory.

Then you need to download a PHP Editor. at this time, I use the Github Atom editor. I personally feel that the interface is very comfortable, the code highlight is also very comfortable, you can download here: https://atom.io. after the download is complete, click install.

Note: There are many PHP versions. The following describes how to support the most commonly used php 5.3.0 and later versions.

Hello World!

Write the simplest Hello World program as follows.

1. create the find_php directory in the application> XAMPP> htdocs directory.

It is not particularly interesting. it is purely for demonstration convenience and does not interfere with the default PHP file.

2. open the Atom editor, use cmd + N to create a file, enter the following code, and run cmd + S to save it to the find_php Directory. the file is named index. php.

  1. Echo 'hello world ';
  2. ?>

If PHP cannot be highlighted as in, you may need to click the bottom right corner of the file to manually specify the syntax highlighting mode of the current file.

3. enter http: // localhost/find_php/index. php in the address bar of the browser to view the Hello World written in PHP.

AppDelegate entry file

IOS applications usually start with an AppDelegate file as the encoding. m ). in PHP, you can use an index. the php file is the only entry to your php program. access and redirection between all your php pages will start from this. the following code can be copied to your index first. php, which implements a basic page access and control framework:

  1. $ Controller = '';
  2. $ Model = array ();
  3. If (isset ($ _ GET ['viewcontroller']) {
  4. $ Controller = $ _ GET ['viewcontroller'];
  5. }
  6. If (isset ($ _ GET ['model']) {
  7. $ Model = $ _ GET ['model'];
  8. }
  9. Echo 'Controller: '. $ controller .'
    ';
  10. Echo 'data model:
    ';
  11. Foreach ($ model as $ key => $ value ){
  12. Echo $ key. ':'. $ value .'
    ';
  13. }
  14. ?>

In the browser address bar, enter: http: // localhost/find_php/index. php? ViewController = HomeViewController & model [id] = 42 & model [name] = iOS122 & model [age] = 25
Page input:

  1. Controller: HomeViewController
  2. Data model:
  3. Id: 42
  4. Name: iOS122
  5. Age: 25

ViewController = follows your representation of your view controller. The model is a dictionary used to store data models. you can enter multiple key-value pairs. IDS, names, and age are all custom keys used to indicate the data you want to transmit to the new page.

Note: Currently, only simple GET requests are considered. Other variants can be written after you are familiar with PHP syntax. at the beginning of learning a new language, you can always get twice the result with half the effort by finding something new and something you have mastered!

MVC design pattern

We will continue to discuss from the common MVC pattern. m, that is, the Model data model, corresponds to the Model we input in the address bar; V, that is, the View, more directly the Display Data. to simplify the discussion, here, we only implement the display of commonly used JSON-format data developed by mobile terminals; C, that is, the Controller, that is, the view Controller we often call, the following describes how to define a view controller in PHP.

Note: The mobile data interface is only one of the application scenarios of PHP. in fact, most of your daily websites are driven by PHP. to write a website with a beautiful layout, you need to learn about HTML and JS. if you are interested, it is recommended to go to this site: http://www.w3school.com.cn

Improved index. php

  1. /* Automatically load class files */
  2. Function _ autoload ($ className ){
  3. If (file_exists ($ className. '. php ')){
  4. Require_once $ className. '. php ';
  5. Return true;
  6. }
  7. Return false;
  8. }
  9. //--------------------------------
  10. /* Obtain information about the page you want to access .*/
  11. $ ControllerName = '';
  12. $ Model = array ();
  13. If (isset ($ _ GET ['viewcontroller']) {
  14. $ ControllerName = $ _ GET ['viewcontroller'];
  15. }
  16. If (isset ($ _ GET ['model']) {
  17. $ Model = $ _ GET ['model'];
  18. }
  19. /* Jump to the specified page .*/
  20. If (''! ==$ ControllerName ){
  21. /* We agree that each controller has at least one $ model attribute and the show method */
  22. $ Controller = new $ controllerName ();
  23. $ Controller-> model = $ model;
  24. $ Controller-> show ();
  25. }
  26. ?>

This method can automatically jump to the corresponding interface based on user input. copy the code directly to index. php, because it does not need to be changed. some technical points are as follows:

The magic method _ autoload is implemented to automatically load related class files. This is similar to introducing a header file globally in. pch, and the entire project is available everywhere.

Php is a weak type language. you do not need to declare a type when defining a variable, but the variable must begin with the dollar sign $.

Php uses the new function to create an object. The syntax is new class name (), which reminds me of the new function in oc. Its syntax is: [class name new];

Functions in php look more like C-language functions, maybe more like blocks in oc, and may be better understood.

The php access attribute uses-> instead of.. the other php access attribute uses obj ['property name'], for example, $ controller ['model'].

Now you access http: // localhost/find_php/index. php? ViewController = HomeViewController & model [id] = 42 & model [name] = iOS122 & model [age] = 25, an error should be reported:

  1. Syntax error, unexpected '>' in/Applications/XAMPP/xamppfiles/htdocs/find_php/HomeViewController. php on line 38

Because you stillNo view controller defined !

Controller: defines the View Controller.

Create the HomeViewController. php file in the find_php folder and copy the following code:

  1. /* It is recommended that a file contains only one class with the same name as the file.
  2. To inherit from other classes, you can use the keyword extends, such */
  3. Class HomeViewController
  4. {
  5. /*
  6. Defines attributes. when defining an attribute, a default value is provided, which is more flexible than OC.
  7. The public keyword is used to specify external access;
  8. Similarly, private (only internal access is allowed) and protected (only access by itself and its subclass is allowed );
  9. The attribute must contain one of the public/private/protected keywords.
  10. */
  11. Public $ model = array (); // defines the attributes that allow external access.
  12. /* Constructor, equivalent to the init initialization method;
  13. This method is automatically called when the New function is called to create an object;
  14. Array indicates the parameter type, $ model is a real parameter, $ model = array (), used to specify the default parameter;
  15. The default parameter is specified. this parameter can be left blank during the call;
  16. The public keyword is equivalent to the attribute keyword. it can be left blank by default. if it is left blank, it is public;
  17. */
  18. Public function _ construct (array $ model = array ())
  19. {
  20. /* Use the $ this keyword to access the attributes of objects within the instance method, and there is no dollar sign before the attribute name $;
  21. Similar to self in oc, but '->' is used instead '.'*/
  22. $ This-> model = $ model;
  23. }
  24. /*
  25. The Destructor works much like dealloc in oc.
  26. */
  27. Public function _ destruct ()
  28. {
  29. $ This-> model = NULL;
  30. }
  31. /* Obtain the content for output display .*/
  32. Protected function getContent ()
  33. {
  34. /* User input is returned in JSON format by default */
  35. $ Content = json_encode ($ this-> model );
  36. Return $ content;
  37. }
  38. /*
  39. Define the instance method: show;
  40. The definition method uses the keyword function and cannot specify the return value, which is not as convenient as oc;
  41. */
  42. Public function show ()
  43. {
  44. /* Use the $ this keyword to call another instance method .*/
  45. $ Content = $ this-> getContent ();
  46. Echo $ content;
  47. }
  48. }

Now you access http: // localhost/find_php/index. php? ViewController = HomeViewController & model [id] = 42 & model [name] = iOS122 & model [age] = 25, the output should be:

  1. {"Id": "42", "name": "iOS122", "age": "25 "}

Note: The page is indeed redirected to the HomeViewController controller and valid output, and the output is the json format data most frequently accessed by mobile development.

The above code fully demonstrates several of the most common functions of php as an object-oriented language (OOP), such as defining attributes and defining instance methods, access attributes and instance methods in the example method. PHP, as a weak type of OOP language, also has some very powerful features. we recommend that you read:

Heavy load

Magic

Static binding later

Model: description of the data Model.

For the M discussion in MVC, here I select the most basic one: M refers to the instance of the class used to store certain data. it can be used for formatting, storage, and transmission of data, but should not include operations such as initiating network requests and reading/writing databases;

In the Model discussed in this article, we further simplified the Model, which can be used only to define the Model of a controller through URL;

PHP is a weak type language, so you do not have to specify a type of Model for a controller.

"Arrays in PHP are actually an ordered ING. Valuing is a type that associates values with keys. This type has been optimized in many aspects, so it can be considered as a real array, or a list (vector), a hash list (an implementation of ING), a dictionary, a set, stack, queue, and more possibilities. Because the value of an array element can be another array, the tree structure and multi-dimensional array are also allowed. "

View: an example of HTML display.

The returned data in JSON format meets the needs of mobile development. However, the html syntax is still used to display the data for better understanding. replace HomeViewController with the following code. the getContent method of the php file:

  1. /* Obtain the content for output display .*/
  2. Protected function getContent ()
  3. {
  4. $ Content ='
      ';
    • Foreach ($ this-> model as $ key => $ value ){
    • $ Content. ="
    • $ Key: $ value
    • ";
    • }
    • $ Content. ='
    ';
  5. Return $ content;
  6. }

Now you access http: // localhost/find_php/index. php? ViewController = HomeViewController & model [id] = 42 & model [name] = iOS122 & model [age] = 25, the output should be:

Id: 42

Name: iOS122

Age: 25

The browser is automatically parsed into a list. the corresponding html code is as follows:

    • Id: 42
    • Name: iOS122
    • Age: 25

Simple html tags are used here.

Summary

By simulating the MVC design pattern of iOS, this article illustrates various related concepts in PHP. familiar with the above operations, you can have basic capabilities to customize server interfaces. participate in the discussion, see: http://www.ios122.com/tag/php/ more comprehensive information, see PHP official Chinese documentation: http://ua2.php.net/manual/zh/langref.php.

IOS, PHP, quot

Related Article

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.