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- /* Implement automatic loading of class files*/
- function __autoload($className) {
- if (file_exists($className . '.php' )) {
- require_once $className . '.php';
- return true;
- }
- return false;
- }
- // -------------------------- -----------
- /* Get relevant information about the page the user wants to visit. */
- $controllerName = '';
- $model = array();
- if (isset($ _GET['viewController'])) {
- $controllerName = $_GET['viewController'];
- }
- if (isset($_GET['model'])) {
- $model = $_GET['model'] ;
- }
- /* Jump to the specified page. */
- if ('' !== $controllerName) {
- /* We agree that each controller has at least one $model attribute and show method */
- $ controller = new $controllerName();
- $controller->model = $model;
- $controller->show();
- }
- ?>
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:
- 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:
- /* It is recommended that there be only one class with the same name as the file.
- If you need to inherit from other classes, you can use the keyword extends, such as */
- class HomeViewController
- {
- /*
- to define attributes. When definition is allowed, give the attribute a default value, which is more flexible than OC.
- The public keyword is used Specify external access;
- Similarly, there are private (only internal access is allowed), protected (only itself and its subclasses are allowed to be accessed);
- There must be one of the keywords public/private/protected before the attribute.
- */
- public $model = array(); // Define attributes that allow external access.
- /* Constructor, equivalent to the init initialization method;
- When the New function is called to create a new object, this method will be automatically called;
- array specifies the parameters Type, $model is the actual parameter, $model = array(), used to specify the default parameters;
- Specifies the parameters of the default parameters, and does not need to be passed when calling;
- The public keyword is equivalent to the keyword of the attribute, the default You can not pass it, otherwise it will be public;
- */
- public function __construct(array $model = array())
- {
- /* To access the properties of the object inside the instance method, use the $this keyword and precede the property name No dollar sign $;
- Similar to self in oc, but using `->` instead of `.` */
- $this->model = $model;
- }
-
- /*
- destructor , the function is very similar to dealloc in oc.
- */
- public function __destruct()
- {
- $this->model = NULL;
- }
-
- /* Get the content for output display. */
- protected function getContent()
- {
- /* Return user input in JSON format by default*/
- $content = json_encode($this->model);
-
- return $content;
- }
-
- /*
- Define instance method: show ;
- The keyword function is used to define the method, and the return value cannot be specified, which is not as convenient as oc;
- */
- public function show()
- {
- /* Use the $this keyword to call another instance method. * /
- $content = $this->getContent();
-
- echo $content;
- }
- }
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:
- {"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:
- /* Get content for output display. */
- protected function getContent()
- {
- $content = '
-
- foreach ($this-> ;model as $key => $value) {
- $content .= "
- $key:$value
";
- }
-
- $content .= ' body>';
-
- return $content;
- }
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.