Zend Framework教程之模型Model用法简单实例
本文实例讲述了ZendFramework教程之模型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=1 phpSettings.display_errors=1 includePaths.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=1 phpSettings.display_errors=1 [development:production] phpSettings.display_startup_errors=1 phpSettings.display_errors=1 resources.frontController.params.displayExceptions=1
/model_demo1/application/controllers/IndexController.php
<?php classIndexControllerextendsZend_Controller_Action{ publicfunctioninit(){ /*Initializeactioncontrollerhere*/ } publicfunctionindexAction(){ 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=newZend_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
<?php classModel_ModelTest{ staticfunctionechoModelModelTest(){ echo'Model_ModelTest<br/>'; } }
/model_demo1/application/models/Test.php
<?php classApplication_Model_Test{ staticpublicfunctiongetUserInfo(){ returnarray( 'user_name'=>'张三', 'user_gender'=>'男' ); } }
/model_demo1/application/Bootstrap.php
<?php classBootstrapextendsZend_Application_Bootstrap_Bootstrap{ protectedfunction_initAutoload(){ $app=$this->getApplication(); $namespaces=array( 'AppTest' ); $app->setAutoloaderNamespaces($namespaces); return$app; } }
/model_demo1/library/app/Test.php
<?php classApp_Test{ staticpublicfunctionechoAppTest(){ echo'App_Test<br/>'; } }
/model_demo1/library/AppTest/Test.php
<?php classAppTest_Test{ staticpublicfunctionechoAppTestTest(){ echo'AppTestTest<br/>'; } }
/model_demo1/library/AppTest2/Test.php
<?php classAppTest2_Test{ staticpublicfunctionechoAppTest2Test(){ echo'AppTest2Test<br/>'; } }
/model_demo1/library/myApp/Test.php
<?php classMyApp_Test{ staticpublicfunctionechoAMyAppTest(){ echo'MyApp_Test<br/>'; } }
/model_demo1/library/Zend/Test.php
<?php classZend_Test{ staticpublicfunctionechoZendTest(){ echo'ZendTest<br/>'; } }
没有贴出的代码,是创建项目默认的代码。
记住:遵循约定规则,就会避免不必要的麻烦。
更多关于zend相关内容感兴趣的读者可查看本站专题:《ZendFrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。