做习惯了编译语言,转到php 使用 php的面向对象开发时候遇见一个挺别扭的问题。在Php中引入对象 后 在调用过程中还需要将对象所在的php文件

require 到当前php文件

 目前代码结构


index.php
<?php

use model\BookModel;

include_once __DIR__.'/autoloader.php';
Autoloader::register(); $book=new BookModel();
$book->id=10;
$book->name="wangk";
echo $book->toString(); ?>
autoloader.php
<?php

/**
*
* 自动载入函数
*/
class Autoloader
{
/**
* 向PHP注册在自动载入函数
*/
public static function register()
{
spl_autoload_register(array(new self, 'autoload'));
} /**
* 根据类名载入所在文件
*/
public static function autoload($className)
{ // DIRECTORY_SEPARATOR:目录分隔符,linux上就是’/’ windows上是’\’
$filePath = __DIR__ . DIRECTORY_SEPARATOR . $className;
$filePath = str_replace('\\', DIRECTORY_SEPARATOR, $filePath) . '.php';
if (file_exists($filePath)) {
require_once $filePath;
return;
// if(method_exists($className, "init")) {
// call_user_func(array($className, "init"), $params);
// }
} else {
echo "无法加载" . $filePath;
} }
}
bookmodel.php
<?php
/**
* Created by PhpStorm.
* User: wangk
* Date: 2015/7/16
* Time: 10:14
*/ namespace model; class BookModel { public $name;
public $id;
public $age; public function toString(){ return 'name:'.$this->name.','.$this->id.",".$this->age;
} }

最新文章

  1. EasyPR--开发详解(2)车牌定位
  2. 必须掌握的八个cmd 命令
  3. asp.net fileupload上传大文件时提示404.13错误
  4. Trianglify - 生成五彩缤纷的 SVG 背景图案
  5. 玉渊潭赏樱花有感:从无到有写一个jQuery开源插件
  6. Docker探索系列1之docker入门安装与操作
  7. [转]redis 五种数据类型的使用场景
  8. HDU 5794 A Simple Chess (容斥+DP+Lucas)
  9. 使用SharePoint 2010的母版页
  10. JAR WAR EAR包的区别
  11. Keil C51必须注意的一些有趣特性
  12. Windows 下 Ionic 开发环境搭建
  13. 小白月赛13 小A的柱状图 (单调栈)
  14. MySQL:(二)
  15. Install Oracle Database client in silent mode
  16. JSON Support in PostgreSQL and Entity Framework
  17. PHP02
  18. 《Head First 设计模式》例子的C++实现(1 策略模式)
  19. ML(1)——机器学习简述
  20. .Net(c#)加密解密工具类:

热门文章

  1. Why we need model on Django ?
  2. atitit.元编程总结 o99
  3. paip.提升效率--僵尸代码的迷思
  4. iOS开发-图片高斯模糊效果
  5. 安装Vmware workstation虚拟机(含软件和注册码)
  6. javaweb学习总结(二十)——JavaBean总结
  7. Leetcode 160 Intersection of Two Linked Lists 单向链表
  8. Memcached常规应用与分布式部署方案
  9. 密码太多记不住?SSO帮你轻松访问VDI及外部资源
  10. 使用Git Bash for Windows