Zend Framework教程之模型Model用法簡單一實例_php執行個體

來源:互聯網
上載者:User

本文執行個體講述了Zend Framework教程之模型Model用法。分享給大家供大家參考,具體如下:

附一個簡單粗俗的例子。只是大概說明了用法:如果要深究,可以自己跟蹤源碼瞭解。

model_demo1

│  .project
│  .buildpath
│  .zfproject.xml
│
├─.settings
│      org.eclipse.php.core.prefs
│      .jsdtscope
│      org.eclipse.wst.jsdt.ui.superType.name
│      org.eclipse.wst.jsdt.ui.superType.container
│
├─application
│  │  Bootstrap.php
│  │
│  ├─configs
│  │      application.ini
│  │
│  ├─controllers
│  │      IndexController.php
│  │      ErrorController.php
│  │
│  ├─models
│  │      Test.php
│  │      ModelTest.php
│  │
│  └─views
│      ├─scripts
│      │  ├─index
│      │  │      index.phtml
│      │  │
│      │  └─error
│      │          error.phtml
│      │
│      └─helpers
├─docs
│      README.txt
│
├─library
│  ├─app
│  │      Test.php
│  │
│  ├─myApp
│  │      Test.php
│  │
│  ├─Zend
│  │      Test.php
│  │
│  ├─AppTest
│  │      Test.php
│  │
│  └─AppTest2
│          Test.php
│
├─public
│      index.php
│      .htaccess
│
└─tests
    │  phpunit.xml
    │  bootstrap.php
    │
    ├─application
    │  └─controllers
    │          IndexControllerTest.php
    │
    └─library

如下是從上到下,每一個檔案的源碼,不再詳細說明:

/model_demo1/application/configs/application.ini

[production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1includePaths.library = APPLICATION_PATH "/../library"bootstrap.path = APPLICATION_PATH "/Bootstrap.php"bootstrap.class = "Bootstrap"appnamespace = "Application"autoloadernamespaces.app = "App_"autoloadernamespaces.my = "MyApp_"resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"resources.frontController.params.displayExceptions = 1[staging : production][testing : production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1[development : production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1resources.frontController.params.displayExceptions = 1

/model_demo1/application/controllers/IndexController.php

<?phpclass IndexController extends Zend_Controller_Action {  public function init() {    /* Initialize action controller here */  }  public function indexAction() {    var_dump ( Application_Model_Test::getUserInfo () );    App_Test::echoAppTest ();    MyApp_Test::echoAMyAppTest ();    Zend_Test::echoZendTest ();    AppTest_Test::echoAppTestTest ();    $auto_loader = Zend_Loader_Autoloader::getInstance();    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(        'basePath' => '/www/model_demo1/application',        'namespace' => '',        'resourceTypes' => array(            'model' => array(                'path' => 'models',                'namespace' => 'Model'            )        )    )    );    $auto_loader->pushAutoloader($resourceLoader);    $auto_loader->registerNamespace(array('AppTest2_'));    AppTest2_Test::echoAppTest2Test();    Model_ModelTest::echoModelModelTest();    exit ();  }}

/model_demo1/application/models/ModelTest.php

<?phpclass Model_ModelTest{  static function echoModelModelTest(){    echo 'Model_ModelTest<br/>';  }}

/model_demo1/application/models/Test.php

<?phpclass Application_Model_Test {  static public function getUserInfo() {    return array (        'user_name' => '張三',        'user_gender' => '男'    );  }}

/model_demo1/application/Bootstrap.php

<?phpclass Bootstrap extends Zend_Application_Bootstrap_Bootstrap {  protected function _initAutoload() {    $app = $this->getApplication ();    $namespaces = array (        'AppTest'    );    $app->setAutoloaderNamespaces ( $namespaces );    return $app;  }}

/model_demo1/library/app/Test.php

<?phpclass App_Test {  static public function echoAppTest() {    echo 'App_Test<br/>';  }}

/model_demo1/library/AppTest/Test.php

<?phpclass AppTest_Test{  static public function echoAppTestTest(){    echo 'AppTestTest<br/>';  }}

/model_demo1/library/AppTest2/Test.php

<?phpclass AppTest2_Test{  static public function echoAppTest2Test(){    echo 'AppTest2Test<br/>';  }}

/model_demo1/library/myApp/Test.php

<?phpclass MyApp_Test {  static public function echoAMyAppTest() {    echo 'MyApp_Test<br/>';  }}

/model_demo1/library/Zend/Test.php

<?phpclass Zend_Test{  static public function echoZendTest(){    echo 'ZendTest<br/>';  }}

沒有貼出的代碼,是建立項目預設的代碼。

記住:遵循約定規則,就會避免不必要的麻煩。

更多關於zend相關內容感興趣的讀者可查看本站專題:《Zend FrameWork架構入門教程》、《php優秀開發架構總結》、《Yii架構入門及常用技巧總結》、《ThinkPHP入門教程》、《php物件導向程式設計入門教程》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.