thinkphp programming for facets-behavioral development

Source: Internet
Author: User

Thinkphp's CBD model

The core retains the most critical parts, and in the important place labels to mark, the other functions are in the way of behavior extension and drive combination, the developer can according to their own needs, a label location to expand or replace the behavior, you can easily customize the framework of the bottom, You can also add your own label locations and add app behaviors at the application level. The label location is similar to the "tangent" in the AOP concept, where the behavior is programmed around this "slice".

CBD is mainly composed of core (core), Behavior (behavior) and drivers (drive). In thinkphp, core refers to the most important core function libraries, class libraries, and configuration files, such as the routing configuration, the underlying model class, and the behavior label configuration used in behavior. Behavior is a key mechanism in the framework, in the thinkphp is the implementation of the "hook" function, the appropriate use can effectively reduce the business logic between the coupling degree, the following will be detailed. The drivers is feature-based and provides drivers for the framework's functional expansion, such as database-driven, cache-driven.

Behavior (behavioral) AOP

At runtime, the programming idea of dynamically cutting code into a class, at a specified location, is a tangent-oriented programming http://hejiajunsh.iteye.com/blog/1776569

AOP (Aspect oriented program) does not conflict with traditional object-oriented programming, but it can complement OOP programming, increase the reusability of some code, reduce the coupling between different business logic, make team development easier and reduce the development cycle effectively.

hook function

Hooks are a common concept in programming and very important. It makes the system very easy to expand (without having to understand its internal implementation mechanism, which can reduce a lot of effort).
It can be understood that when a glass ball falls from the air and is about to hit a person, an event occurs early. For example, tell the person who was smashed that the ball is already in the process of falling,
Tell is an event, a hook, we can target different people to make different corresponding, if it is a man we tell him that the ball hit people do not hurt, if it is a woman told her very painful;

The hook function can intercept and process messages from other applications. Whenever a particular message is issued, the hook program captures the message before it reaches the destination window, i.e. the hook function gets control first. At this point the hook function can process (change) the message, or you can continue to pass the message without processing, and you can force the end of the message delivery.

The process of setting the hook is actually setting the event-driven process, which is simply to define the hook function, register the event, listen (Mount), meet the trigger conditions and trigger the hook function.

Behavioral Outreach

Behavior (Behavior) is a key extension of the thinkphp extensibility mechanism, where the behavior can be called independently, or can be bound to a tag (bit) for listening. The behavior here refers to a relatively abstract concept, you can imagine in the application execution process of an action or processing, in the framework of the execution process, each location can have behavior, such as route detection is a behavior, static caching is a behavior, user rights detection is also behavior, large to the business logic, Small to browser detection, multi-language detection and so can be regarded as a behavior, even say you want to give your site users the first access to pop hello,world! These can be seen as a behavior where the presence of the behavior allows you to change or add functionality to the perimeter by extending or configuring it without altering the framework and application.

In other words, behavior in the TP framework implements AOP (aspect-oriented) programming, the binding tag, which sets the trigger point using the hook function.

Behavior Extension Add process
    • Creating the Behavior Class

Can be placed directly in the think\behaviors, can also be placed in the application directory (application\common\behavior), new file, named format is the label name +behavior.class.php . Note that the class must contain the Run method, and the default run method is the Ingress method.

Code content

namespace Common\Behavior;use Think\Behavior;class TolldetectBehavior extends Behavior{ //方法名必须为run,作为入口文件 public function run(&$param){ echo "Hello "; }}
    • Register label (Mount)
      There are two types of label registration methods in thinkphp, one is manual registration and one is auto-registration.

The following is the source code for the manual registration method Add ()

/** * Dynamically add plugins to a tag * @param string $tag tag name *  @param mixed $name plug-in name *  @return void */static public function add ($tag, $ Name) {if (! Isset (self:: $tags [$tag]) {self:: $tags [$tag] = array (); } if (Is_array ($name)) {self:: $tags [$tag] = Array_merge (self:: $tags [$tag], $name); }else{self:: $tags [$tag] = $name;}}     

We can bind with Hook::add ($tags, $name) at the time of invocation. Note Tags are the name of the tag to bind to, $name for the specific behavior behavior file.

If you are using autoenrollment, you will need to create a new tags.php file in the common directory. It is possible to bulk bind directly by returning the array, and the TP will automatically load the configuration and bind it in the run. For example:

<?php return array( "action_begin" => array(‘Home\\Behaviors\\testBehavior‘)); ?>

thinkphp programming for facets-behavioral development

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.