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.:

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:// 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 and use the cmd+NCreate a new file, enter the following code, and cmd+SSave to the find_php directory, and the file is named index.php.
<?phpecho ‘Hello World‘;?>

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:

<?php$controller =‘‘;$model =Array ();if (Isset$_get[' Viewcontroller ')) {$controller =$_get[' Viewcontroller '];}if (isset ($_get[ ' model '))) { $model = $_get[ ' model ';} echo  ' controller: '.  $controller.  ' <br/> '; echo  ' data model: <br/> ';  foreach ( $model as  $key = Span class= "hljs-variable" > $value) {echo  $key. ": '.  $value.  ' <br/> ';} ?>             

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:


viewController=followed by you. Your representation of your view controller, model is a dictionary that is used to store data models, and supports the input of multiple key-value pairs., and so on are id name age custom keys that represent the data you want to pass to the new page, if no can be written.

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:

The improved index.php
<?phpindex.php/* Implement automatic loading of class files */function__autoload($className) {if (File_exists ($className.'. php ')} {Require_once$className.'. php ';ReturnTrue }ReturnFalse }// --------------------------------/* Get information about the page that the user wants to access. */$controllerName =‘‘;$model =Array ();if (Isset$_get[' Viewcontroller ')) {$controllerName =$_get[ ' Viewcontroller '];} if (isset ($_GET[ model ')) { $model = $_get[ ' model ');} /* jumps to the specified page. */if ( "!==  $controllerName) {/* we agree that each controller has at least one $model property and Show method */ $controller = new  $controllerName ();  $controller->model =  $model;  $controller->show (); ?>             

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, and the syntax is that new 类名() it reminds me 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 properties, using -> , instead of, . another way to access properties in PHP using obj[‘属性名‘] , such as $controller[' model '.

When you visit http://localhost/find_php/index.php?viewController=HomeViewController&model[id]=42&model[name]=iOS122&model[age]=25 , you should get an error:

‘>‘ in /Applications/XAMPP/xamppfiles/htdocs/find_php/HomeViewController.php on line 38

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:

<?phphomeviewcontroller.php/* Only one class with the same name as the file is recommended. If you need to inherit from another class, you can use the keyword extends, such as */Classhomeviewcontroller{/* Define properties, allow definition, give property a default value, which is more flexible than OC. The public keyword is used to specify externally accessible; Similarly there is private (allow internal access only), protected (only allow itself and its subclasses to access); You must have one of the keyword public/private/protected before the property. */Public$model =Array ();Defines properties that allow external access./* constructor, equivalent to Init initialization method; This method is automatically called when a new object is called by the new function; Array indicates the parameter type, $model is an argument, $model = Array (), which specifies the default parameter; The parameters of the default parameters are specified, and can not be transmitted when called; The Public keyword function is the same as the keyword of the property, the default can not be passed, not the public; */Publicfunction__construct(Array$model = array()) {/* Access the properties of the object inside the instance method, using the $this keyword, with no dollar sign before the property name $; Similar to self in OC, but using '--' instead of '. ' */$this->model =$model; }/* destructors, which act like Dealloc in OC. */Publicfunction__destruct() {$this->model =NULL; }/* Get content for output display. */protected function getcontent () {/* default to return user input in JSON format */ $content = Json_encode ( $this->model); return  $content;} /* Define instance method: Show; The definition method uses the keyword function and cannot specify the return value, which is not as convenient as OC; */function show () {/* uses the $this keyword to invoke another instance method. */ $content =  $this->getcontent (); echo  $content;}        

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


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:

 /* get content for output display. */ protected function getContent< Span class= "Hljs-params" > () { $content =  foreach ( $this->model as Span class= "hljs-variable" > $key =  $value) { $content. Span class= "hljs-string" > "<li> $key: $value </li>"; }  $content. =  ' </ul></body>return  $content;}        

At this point 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:

<Html><head></ head><body> <ul>< Li>id:42</li><li>name:ios122</li> <li>age:25</ li></ul> </body></ HTML>             

A simple HTML tag is used here.


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: For more comprehensive information, see the official PHP Chinese document:


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: 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.