iOS program ape How to quickly master PHP, Avatar " full stack siege lion "

Source: Internet
Author: User
Tags access properties php server php editor

This is a PHP getting Started guide to the vast iOS program ape from an iOS developer's perspective. In this article I try to explore the commonality between Objectiv-c and PHP to help a siege lion with some iOS development experience to quickly get started with a background development language. The background development language is the "data Interface "in the form of the thing that appears in our development documentation! Mastering PHP can be a great benefit for your current iOS development work or for the long-term development of your personal career. Most importantly, PHP is not a toy language in itself, but a background development language that is still in use by a considerable part of the company. Even your current company; This article is not a simple basic manual, but rather a way to understand the most important and most common concepts and functions in PHP in a manner better suited to iOS developers. Reading and effectively practicing this article will help you to have the ability to write a background data interface independently.

necessary preparation and instructions

First, you need to download the latest version of the XAMPP software to build a PHP server locally. Download Address: https://www.apachefriends.org/download.html.

When the download is complete, double-click Install. After the installation is successful, select Mange Servers-->start all to start the local server. After successful startup, you can see a default PHP page by entering http://localhost in the browser.

Your PHP server files are placed by default in the: Application-->xampp-->htdocs directory.

And then you need to download a PHP editor, and I'm using the GitHub Atom editor. Personal feeling interface is very comfortable, code highlighting is also very comfortable, you can download here: Https://atom.io. When the download is complete, click Install.

The final note is: PHP version a lot, the following explanation supports the most commonly used PHP 5.3.0 and above version.

Hello world!

Write the simplest Hello world program below and follow the steps below.

1. Create a new directory find_php in the application-->xampp-->htdocs directory.

No special meaning, pure shredding is for demonstration convenience, and does not interfere with the default existing PHP files.

2. Open the Atom editor, use Cmd+n to create a new file, enter the following code, and Cmd+s save to the find_php directory with the file named index.php.
    1. Echo ' Hello world ';
    2. ?>
Copy Code

If PHP cannot be highlighted as medium, you may need to click in the lower-right corner of the file to manually specify the syntax highlighting for the currently asked file.

3. In the Browser address field, enter: http://localhost/find_php/index.php, you can see the Hello World written in PHP.

Appdelegate Portal File

iOS apps usually start with the Appdelegate file as the encoding (MAIN.M, not scrutiny). In PHP, you can use a index.php file as the only entry for your PHP program. All of your PHP pages are accessed with jumps, Will be started here. The following code can be copied to your index.php first, it 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. ?>
Copy Code

Then in the browser address bar, type: 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
Copy Code

Viewcontroller= followed by you. Your representation of your view controller, model is a dictionary for storing data models, supporting the input of multiple key-value pairs. Id,name,age and so on are all custom keys that represent the data you want to pass to the new page, if you don't write it.

Note: Only simple get requests are considered here, and other variants can be written on your own after you are familiar with PHP syntax. In the early days of learning a new language, it is always possible to find new things in common with what you already know.

MVC design Pattern

We are still starting with the usual MVC pattern to start a further discussion. M, the model data models, corresponds to what we enter in the address bar; V, that is, views view, more directly is the display of data, in order to simplify the discussion, we only in the mobile development of commonly used JSON format data display as a realization; C, controller controllers, which we often call view controllers, discuss how to define a view controller in PHP.

Note: The mobile data interface is just one of the PHP application scenarios. In fact, your daily contact with the absolute part of the site is driven by PHP, to write a beautiful layout of the site, you need to learn HTML and JS related knowledge. If interested, suggest going to this site: http://www.w3school.com.cn

The improved index.php
    1. !--? PHP//index.php /* Implement automatic loading of class files */
    2. function __autoload ($className) {
    3. if (file_e Xists ($className. '. php ') {
    4. require_once $className. '. php ';
    5. return true;
    6. }
    7. return false;
    8. }
    9. //--------------------------------
    10. /* Get information about the page that the user wants to access. */
    11. $co Ntrollername = ';
    12. $model = Array ();
    13. if (isset ($_get[' Viewcontroller ')) {
    14. $controllerName = $_get[' Viewcontroller '];
    15. }
    16. if (isset ($_get[' model ')) {
    17. $model = $_get[' model '];
    18. }
    19. /* Jumps to the specified page. */
    20. if ('!== $controllerName) {
    21. /* We agree that each controller has at least one $model property and S How method */
    22. $controller = new $controllerName ();
    23. $controller->model = $model;
    24. $controller->show ();
    25. }
    26. ?
Copy Code

This method can be used to automatically jump to the corresponding interface according to user input. You can simply copy the code into the index.php, as it will no longer need to be changed. Some technical points are:

The Magic method __autoload is implemented to implement automatic loading of related class files. It's kind of like we're introducing a header file globally in a. PCH, and then the entire project is available.

PHP is a weakly typed language, and you do not have to declare the type when you define the variable, but the variable starts with the dollar sign $.

PHP uses the new function to create an object, the syntax is the new class name (), which makes me think of the new function in OC, whose syntax is: [Class name New];

The function in PHP looks more like a C-language function, perhaps more like a block in OC, probably better understood.

PHP accesses the attribute, using the-> Another way for PHP to access properties is to use the obj[' property name ', such as $controller[' model '.

At this point you visit http://localhost/find_php/index.php?viewcontroller=homeviewcontroller&model[id]=42&model[name]= IOS122&MODEL[AGE]=25, you should get an error:

    1. Syntax error, unexpected ' > ' in/applications/xampp/xamppfiles/htdocs/find_php/homeviewcontroller.php on line 38
Copy Code

Because you haven't defined the View controller yet !

Controller: Define the View Controller

Create a new homeviewcontroller.php file in the find_php folder and copy the following code in:

  1. /* Only one class with the same name as the file is recommended in a file.
  2. If you need to inherit from another class, you can use the keyword extends, such as */
  3. Class Homeviewcontroller
  4. {
  5. /*
  6. Defines a property that allows a default value to be given to a property when defined, which is more flexible than OC.
  7. The public keyword is used to specify externally accessible;
  8. Similarly there is private (allow internal access only), protected (only allow itself and its subclasses to access);
  9. You must have one of the keyword public/private/protected before the property.
  10. */
  11. Public $model = Array (); Defines properties that allow external access.
  12. /* constructor, equivalent to Init initialization method;
  13. This method is automatically called when a new object is called by the new function;
  14. Array indicates the parameter type, $model is an argument, $model = Array (), which specifies the default parameter;
  15. The parameters of the default parameters are specified, and can not be transmitted when called;
  16. The Public keyword function is the same as the keyword of the property, the default can not be passed, not the public;
  17. */
  18. Public function __construct (array $model = Array ())
  19. {
  20. /* Access the properties of the object inside the instance method, using the $this keyword, with no dollar sign before the property name $;
  21. Similar to self in OC, but using '--' instead of '. ' */
  22. $this->model = $model;
  23. }
  24. /*
  25. destructors, which act like Dealloc in OC.
  26. */
  27. Public Function __destruct ()
  28. {
  29. $this->model = NULL;
  30. }
  31. /* Get content for output display. */
  32. protected function getcontent ()
  33. {
  34. /* The user input is returned in JSON format by default */
  35. $content = Json_encode ($this->model);
  36. return $content;
  37. }
  38. /*
  39. Define an instance method: show;
  40. The definition method uses the keyword function and cannot specify the return value, which is less convenient than OC;
  41. */
  42. Public Function Show ()
  43. {
  44. /* Use the $this keyword to invoke another instance method. */
  45. $content = $this->getcontent ();
  46. Echo $content;
  47. }
  48. }
Copy Code

At this point you visit http://localhost/find_php/index.php?viewcontroller=homeviewcontroller&model[id]=42&model[name]= IOS122&MODEL[AGE]=25, the output should be:

    1. {"id": "All", "name": "iOS122", "Age": "25"}
Copy Code

Note that the page did jump to the Homeviewcontroller controller and output it efficiently, and the output was the data in the JSON format that we developed most frequently in contact with the mobile side.

The above code shows the most common features of PHP as an object-oriented (OOP) language, such as defining attributes, defining instance methods, accessing attributes and instance methods within an example method, and so on. PHP as a weak type of OOP language, there are some very powerful features, recommended to read:

Overload

Magic method

Late static binding

Model: A few notes about the data model.

Online discussion of M in MVC, here I chose the most basic one: M refers to an instance of a class used to store a certain kind of data. It can be used to format data storage and delivery, but should not include initiating network requests and read-write database operations;

In the model discussed in this article, we further simplified the model, allowing and allowing only the model to be used to define a controller via a URL;

PHP is a weakly typed language, so you don't have to specify some type of model specifically for a particular controller.

"The array in PHP is actually an ordered map. A map is a type that associates values to the keys. This type is optimized in many ways, so it can be used as a real array, or as a list (vector), a hash (an implementation of a mapping), a dictionary, a collection, a stack, a queue, and more possibilities. Because the value of an array element can also be another array, the tree structure and the multidimensional array are also allowed. "

View: A display instance of HTML.

Returning data in JSON format has reached the need for mobile development, but it still uses HTML syntax to display the data for better understanding. Replace the GetContent method of the homeviewcontroller.php file with the following code:

    1. /* Get 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. }
Copy Code

At this point you visit 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
Copy Code

A simple HTML tag is used here.

Summary

This paper provides an overview of the corresponding concepts in PHP by simulating the design patterns of MVC in IOS. Being familiar with the above actions gives you the basic ability to customize the server interface. To participate in the discussion, see: http://www.ios122.com/tag/php/ For more comprehensive information, see the official PHP Chinese document: http://ua2.php.net/manual/zh/langref.php.

IOS, PHP, quot
  • 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.