Home > Backend Development > PHP Tutorial > How can iOS programmers quickly master PHP and become a 'full-stack siege lion'?

How can iOS programmers quickly master PHP and become a 'full-stack siege lion'?

WBOY
Release: 2016-07-23 08:54:45
Original
859 people have browsed it

This is an introductory guide to PHP written from the perspective of an iOS developer for iOS programmers. In this article, I strive to explore the commonalities between objectiv-c and PHP to help developers with certain iOS development experience. Let's quickly get started with a back-end development language. The back-end development language is what appears in our development documents in the form of "data interface"! Mastering PHP is essential for your current iOS development work and your future career. It will be of great benefit to long-term development! The most important thing is that PHP itself is not a toy language, but a backend development language still used by quite a few companies, even your current company; this article is not a simple one. This is a basic manual, but it systematically explains the core, most important and commonly used concepts and functions in PHP in a way that is more suitable for iOS developers to understand. Reading and effectively practicing this article will help you have the ability to independently write background data interfaces. Ability.

Necessary preparation and instructions

How can iOS programmers quickly master PHP and become a 'full-stack siege lion'?

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

After the download is completed, double-click to install. After the installation is successful, select Mange Servers-->Start All to start the local server. After the startup is successful, enter http://localhost in the browser and you will see a default PHP page.

Your php server files are placed by default in: Application-->XAMPP-->htdocs directory.

Then you also need to download a PHP editor. At this time, I use Github's Atom editor. Personally, I feel that the interface is very comfortable, and the code highlighting is also very comfortable to look at. You can download it here: https://atom.io .After the download is completed, click to install.

The last thing to note is: There are many versions of PHP. The following explanation supports the most commonly used version of php 5.3.0 and above.

Hello World!

Write the simplest Hello World program below, please follow the steps below.

1. Create a new directory find_php in the Application-->XAMPP-->htdocs directory.

No special meaning, it is purely for the convenience of demonstration and does not interfere with the default php files.

2. Open the Atom editor, use cmd+N to create a new file, enter the following code, and use cmd+S to save it to the find_php directory. The file is named index.php.
  1. echo 'Hello World';
  2. ?>
Copy the code

If PHP cannot be highlighted as in, you may need to click on the lower right corner of the file to manually specify the syntax highlighting method of the current file.
How can iOS programmers quickly master PHP and become a 'full-stack siege lion'?

3. Enter: http://localhost/find_php/index.php in the browser address bar, and you will see Hello World written in PHP.

How can iOS programmers quickly master PHP and become a 'full-stack siege lion'?

AppDelegate entry file

iOS applications usually start with the AppDelegate file as the starting point for coding (main.m to be precise, I won’t go into detail here). In PHP, you can use an index.php file as the only entrance to your php program. You All access and jumps between PHP pages will start 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.'
    ' ]=iOS122&model[age]=25
  13. Page input:
Controller: HomeViewControllerData model:
id:42

name:iOS122
age:25

Copy code
  1. viewController=Followed by you represents your view controller. Model is a dictionary used to store data models and supports the input of multiple key-value pairs. ID, name, age, etc. are all custom keys. Use Yu means that you want to pass the data to the new page. If there is no data, you don’t need to write it.

    Note: Only simple GET requests are considered here for the time being. As for other variations, you can write them yourself after becoming familiar with PHP syntax. In the early days of learning a new language, you can always try to find the commonalities between new things and things you have already mastered. Get twice the result with half the effort!

    MVC design pattern

    We still start further discussion from the commonly used MVC pattern. M, which is the Model data model, corresponds to the model we entered in the address bar; V, which is the view, more directly displays the data. In order to simplify the discussion, Here we only implement the display of JSON format data commonly used in mobile terminal development; C is the Controller controller, which is what we often call the view controller. The following will discuss in detail how to define the view controller in PHP.

    Note: The mobile data interface is only one of the application scenarios of PHP. In fact, most of the websites you come into contact with daily are driven by PHP. If you want to write a beautifully laid out website, you need to learn HTML and JS related knowledge. If If you are interested, it is recommended to go to this website: http://www.w3school.com.cn

    Improved index.php
    1. /* Implement automatic loading of 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. /* Get relevant information about the page the user wants to visit. */
    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 show method */
    22. $ controller = new $controllerName();
    23. $controller->model = $model;
    24. $controller->show();
    25. }
    26. ?>
    Copy code

    This method can be implemented based on user input Automatically jump to the corresponding interface. You can copy the code directly to index.php, because it no longer needs to be changed for the time being. Some technical points explained are:

    Implemented the magic method __autoload to automatically load related class files. This is somewhat similar to us introducing a header file globally in .pch, and then the entire project is available everywhere.

    php is a weakly typed language. You do not need to declare the type when defining a variable, but the variable must start with a dollar sign $.

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

    The functions in php look more like C language functions, maybe more like blocks in oc, which may be easier to understand.

    When accessing attributes in PHP, use -> instead of .; Another way to access attributes in PHP is to use obj['attribute name'], such as $controller['model'].

    At this time, when you visit 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
    Copy code

    Because you haven’t defined a view controller !

    Controller: Define the view controller

    Create a new HomeViewController.php file in the find_php folder and copy the following code into it:

    1. /* It is recommended that there be only one class with the same name as the file.
    2. If you need to inherit from other classes, you can use the keyword extends, such as */
    3. class HomeViewController
    4. {
    5. /*
    6. to define attributes. When definition is allowed, give the attribute a default value, which is more flexible than OC.
    7. The public keyword is used Specify external access;
    8. Similarly, there are private (only internal access is allowed), protected (only itself and its subclasses are allowed to be accessed);
    9. There must be one of the keywords public/private/protected before the attribute.
    10. */
    11. public $model = array(); // Define attributes that allow external access.
    12. /* Constructor, equivalent to the init initialization method;
    13. When the New function is called to create a new object, this method will be automatically called;
    14. array specifies the parameters Type, $model is the actual parameter, $model = array(), used to specify the default parameters;
    15. Specifies the parameters of the default parameters, and does not need to be passed when calling;
    16. The public keyword is equivalent to the keyword of the attribute, the default You can not pass it, otherwise it will be public;
    17. */
    18. public function __construct(array $model = array())
    19. {
    20. /* To access the properties of the object inside the instance method, use the $this keyword and precede the property name No dollar sign $;
    21. Similar to self in oc, but using `->` instead of `.` */
    22. $this->model = $model;
    23. }
    24. /*
    25. destructor , the function is very similar to dealloc in oc.
    26. */
    27. public function __destruct()
    28. {
    29. $this->model = NULL;
    30. }
    31. /* Get the content for output display. */
    32. protected function getContent()
    33. {
    34. /* Return user input in JSON format by default*/
    35. $content = json_encode($this->model);
    36. return $content;
    37. }
    38. /*
    39. Define instance method: show ;
    40. The keyword function is used to define the method, and the return value cannot be specified, 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. }
    Copy code

    At this time 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":"42","name":"iOS122","age":"25" }
    Copying the code

    shows that the page does jump to the HomeViewController controller and is effectively output; and the output is the json format data that we most often come into contact with in mobile development.

    The above code fully demonstrates several of the most commonly used functions of PHP as an object-oriented (OOP) language, such as defining properties, defining instance methods, accessing properties and instance methods within example methods, etc. PHP is a weakly typed language The OOP language also has some very powerful features. Recommended reading:

    Reload

    Magic method

    Post static binding

    Model: Some notes on the data model.

    In the online discussion about M in MVC, here I choose the most basic one: M specifically refers to an instance of a class used to store certain data. It can be used for formatted storage and transmission of data, but it should not be Including operations such as initiating network requests and reading and writing databases;

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

    PHP is a weakly typed language, so there is no need to specify a certain type of Model for a certain controller.

    "An array in PHP is actually an ordered map. A map is a type that associates values ​​to keys. This type has been optimized in many aspects, so it can be treated as a real array, or list (vector), Hash tables (which are an implementation of maps), dictionaries, sets, stacks, queues and many more possibilities. Since the value of an array element can also be another array, tree structures and multidimensional arrays are also allowed. "

    View: An instance that displays HTML.

    Returning data in JSON format has met the needs of mobile development, but html syntax is still used 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 time, when 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

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

      • < li>id:42
      • name:iOS122
      • age:25
    Copy code

    Simple html tags are used here.

    summary

    This article briefly explains the corresponding concepts in PHP by simulating the MVC design pattern of iOS. Being familiar with the above operations will enable you to have 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 PHP official Chinese documentation: http://ua2.php.net/manual/zh/langref.php.

iOS, PHP, quot


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template