How does iOS program quickly master PHP and turn into a full-stack attacker? 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.
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:
'; Echo' data model:
'; Foreach ($ model as $ key => $ value) {echo $ key.': '. $ value .'
';}?>
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:
Controller: HomeViewController data model: id: 42 name: iOS122age: 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
Model = $ model; $ controller-> show () ;}?>
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:
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:
'Rather '. '*/$ this-> model = $ model;}/* destructor, which is similar to dealloc in oc. */public function _ destruct () {$ this-> model = NULL;}/* get the content for output display. */protected function getContent () {/* returns the user input in JSON format by default */$ content = json_encode ($ this-> model); return $ content ;} /* define the instance method: show; the definition method uses the keyword function and the return value cannot be specified. this is not as convenient as oc; */public function show () {/* use the $ this keyword to call another instance method. */$ content = $ this-> getContent (); echo $ content ;}}
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"}
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:
/* Get the content for output display. */protected function getContent () {$ content ='
'; Foreach ($ this-> model as $ key => $ value) {$ content. ="
- $ Key: $ value
";}$ Content. ='
'; Return $ content ;}
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:
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.