>  기사  >  백엔드 개발  >  thinkPHP 사용자 정의 클래스 구현 method_php 예제에 대한 자세한 설명

thinkPHP 사용자 정의 클래스 구현 method_php 예제에 대한 자세한 설명

WBOY
WBOY원래의
2016-12-05 13:28:251087검색

이 기사의 예에서는 thinkPHP 사용자 정의 클래스 구현 방법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 자세한 내용은 다음과 같습니다.

1. 모델을 통한 통화

<&#63;php
/**
 * 积分模型 api接口
 */
class ApiModel{
  private $url = 'http://js.yunlutong.com/Customer/Interface';
  public function test() {
    $post_data['action']    = 'sadf';
    $post_data['callback']   = '&#63;';
    $res = request_post($this->url, $post_data);
    $firstChar = substr($res,0,1);
    if ($firstChar =='&#63;') {
      $res = substr($res,2);
      $res = substr($res,0,strlen($res)-1);
    } elseif($firstChar == '(') {
      $res = substr($res,1);
      $res = substr($res,0,strlen($res)-1);
    }
    dump(json_decode($res,true));
  }
}

모델을 상속하지 마세요. 그렇지 않으면 테이블이 존재하지 않기 때문에 오류가 보고됩니다.

전화주세요

$Api = D('Api');
$Api->test();

통화는 참 편리하지만 늘 좀 무리한 느낌이 듭니다. 결국 이 D가 데이터베이스를 운영하는 셈이다.

2. 클래스를 도입하여 구현하고 클래스를 ORG에 넣습니다

<&#63;php
class Integral{
  private $url = 'http://js.yunlutong.com/Customer/Interface';
  public function test() {
    $post_data['action']    = 'sadf';
    $post_data['callback']   = '&#63;';
    $res = request_post($this->url, $post_data);
    $firstChar = substr($res,0,1);
    if ($firstChar =='&#63;') {
      $res = substr($res,2);
      $res = substr($res,0,strlen($res)-1);
    } elseif($firstChar == '(') {
      $res = substr($res,1);
      $res = substr($res,0,strlen($res)-1);
    }
    dump($res);
    dump(json_decode($res,true));
  }
}
&#63;>

전화

import("@.ORG.Api.Integral");
$integralApi = new Integral();
$integralApi->test();

구성하고 자동으로 로드

'APP_AUTOLOAD_PATH'   => '@.ORG,@.ORG.Api',

이렇게 하면 Api 폴더에 클래스가 몇 개 있더라도 자동으로 로드되므로 호출하기가 편리하며 단일 참조 가져오기("@.ORG.Api.Integral")가 필요하지 않습니다.

더 많은 thinkPHP 관련 컨텐츠에 관심이 있는 독자는 이 사이트의 특별 주제인 "ThinkPHP 시작하기 튜토리얼", "ThinkPHP 템플릿 작동 기술 요약", "ThinkPHP 공통 메소드 요약", "codeigniter 시작하기 튜토리얼"을 확인할 수 있습니다. , "CI (CodeIgniter) ) 프레임워크 고급 튜토리얼", "Smarty 템플릿 시작을 위한 기본 튜토리얼" 및 "PHP 템플릿 기술 요약".

이 기사가 ThinkPHP 프레임워크를 기반으로 하는 모든 사람의 PHP 프로그래밍에 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.