PHP Series: PHP Web Development Basics, phpweb_php tutorials

Source: Internet
Author: User
Tags autoload php web development readfile

PHP Series: PHP Web Development Basics, Phpweb


PHP is a dynamic type of web development scripting language, PHP with a paging file as the loading and running unit, PHP now has composer as a development package management.

1. Using composer to manage dependencies

Since. NET development with NuGet Management assembly dependencies, I can no longer leave it, fortunately, there are MAVEN management jar package in Java, although the open source China mirror is too slow but there are ibiblio images available, PHP now finally has composer (reference 1) to manage the resource bundle.

(1) Global installation (reference 2)

To execute the command line in the specified installation directory:

" ReadFile (' Https://getcomposer.org/installer '); " | Php

To execute the command line in the specified installation directory:

Echo " %~dp0composer.phar " %*>composer.bat

Adds the specified installation directory to the system variable path of the environment variable.

(2) Project installation

To execute the command line in the project directory:

" ReadFile (' Https://getcomposer.org/installer '); " | Php

Use the Composer.json file to configure dependencies. For example, configure the log components commonly used in PHP:

{    "require": {        "Monolog/monolog": "1.0.*"    }}

Use the command line command to install the dependent resource bundle for PHP Composer.phar installation. If you have a global installation, you only need to perform composert install.

This will generate the Composer.lock file and rely on the download resource bundle to the vendor directory, composer.lock files are the files that are actually used when downloading dependencies.

(3) Upgrade dependency

The project uses the Composer.lock file to maintain dependencies, and if we need to use a newer version of the dependency, you need to modify the Composer.json file as needed and execute the update command. Global Installation: Composer update, Project installation using:php composer.phar update。

(4) Resource bundle query and mirroring

You can use http://packagist.org to retrieve resource bundles, such as https://www.nuget.org/packages/in. NET and http://search.maven.org/in Java.

The http://packagist.phpcomposer.com image can be used, and the global configuration uses the following command-line command:

Composer Config-g Repo.packagist composer http://packagist.phpcomposer.com

The project configuration uses the following command line:

Composer Config Repo.packagist composer http://packagist.phpcomposer.com

The simplest way of course is to modify the Composer.config file directly:

{    "require": {        "Monolog/monolog": "1.0.*"    },    "repositories": {         "Packagist": {            "type": "Composer",            "url": "Http://packagist.phpcomposer.com"         }    }}

(5) Automatic loading

Composert automatically generates and maintains autoload.php files with all dependent information in the vendor directory. So we only need to use third-party libraries through autoload.php. For example:

 
  require ' vendor/autoload.php '; $log New Monolog\logger (' name '); $log->pushhandler (new monolog\handler\streamhandler (' App.log ', Monolog\logger::WARNING) ); $log->addwarning (' Foo ');? >

2.PHP Foundation

(1) PHP script language similar to C language syntax.

If you have a C or C + + language base, then many concepts and grammars can be directly contrasted. From case sensitivity to annotations, from data types, functions, parameter passing, and member references. Learning from the c/c++/java/c# system, plus the basics of JavaScript scripting language, add PHP skills to the next step.

(2) PHP is a dynamic type language.

PHP has the same data type definitions as strongly typed languages like C, supported types, except that the same variable in PHP can represent different types of data at different times, but at the same time, each variable is equivalent to a variable of the specified type in the strongly typed type. So for PHP, the more appropriate salutation is the dynamic type. The habit of using strongly typed languages such as java/c# always worries about the dynamic type of scripting language, and can reduce this concern (is_bool\is_integer\is_double\is_string\is_null, etc.) through PHP type checking functions.

(3) PHP works as a paging file.

In any case, the use of php,php is run as a paging file, which is similar to JavaScript running on the browser side, and JavaScript files can only be referenced and executed if they are loaded into the current page. PHP can only be referenced and executed after it has been loaded by include or required, even if the subsequent additions of __autoload and spl_autoload still simplify rather than change the nature of their loaded references.

(4) PHP does not require a complex framework

PHP is inherently processing Web requests, the content of the HTTP protocol is the focus of PHP, generally speaking, in addition to vertical request processing, horizontal authentication and authorization Control section, the other parts should not be too dependent on PHP, if the use of high-coupling web framework PHP itself is wrong. Due to the way PHP itself is run and loaded, PHP is inherently suitable for reusing various components rather than using frameworks to solve problems. People who hope for the framework all day, either don't want to work on the basis of PHP or do not want to work on the business logic.

3.PHP Common Files

(1) configuration file config.php

The various information about the application configuration is defined in one or more configuration files. Typically, each request loads a configuration file that contains not only database connection information, such as when MVC is applied, and each request loads a configuration file to read the rules between all URLs and controller and view.

(2) Entry file index.php

The file that processes the first page request or all requests. Index.php in the implementation of the PHP MVC is a natural front-section controller Frontcontroller, by index.php responsible for vertical processing request forwarding, horizontal authentication and authorization processing files, etc. also need to be loaded in index.php.

(3) Data Access file database.php

Database.php is the most important file for data access in a tool class, and in order to avoid repetitive code in data access, a typical PHP application will have a separate or set of data access files.

4. Custom session

Customizing the session in PHP is much more convenient than Java, just pass the custom function name as a parameter to Session_set_save_handler ($open, $close, $read, $write, $destroy, $GC) As a parameter, we customize a session that uses a cookie as a store, and as a demonstration there is no validation of the length of the data or decryption using symmetric encryption.

 Phpfunctionopen_session () {return true;}functionclose_session () {return true;}functionRead_session ($id) {    if($_cookie[$id]) {        return Base64_decode($_cookie[$id]); }    return'';}functionWrite_session ($sid,$data) {    if($_cookie[' Session_End ']) {        Setcookie($sid,Base64_encode($data),$_cookie[' Session_End ']); } Else {        $start= Time() +Session_get_cookie_params() [' Lifetime ']; Setcookie(' Session_End ',$start,$start); Setcookie($sid,Base64_encode($data),$start); }    return true;}functiondestroy_session () {$_session=Array(); return true;}functiongc_session () {return true;}Ini_set("Session.cookie_lifetime", "3600");Session_set_save_handler(' Open_session ', ' close_session ', ' read_session ', ' write_session ', ' destroy_session ', ' gc_session ');Session_Start();$_session[' count '] + = 1;Echo $_session[' Count '];Session_write_close();?>

Reference

(1) https://getcomposer.org/doc/00-intro.md

(2) http://docs.phpcomposer.com/00-intro.html

http://www.bkjia.com/PHPjc/1092700.html www.bkjia.com true http://www.bkjia.com/PHPjc/1092700.html techarticle PHP Series: PHP Web Development Foundation, Phpweb PHP is a dynamic type of web development scripting language, PHP with the page file as the loading and running unit, PHP now has the composer as a development guarantee ...

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