博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

php笔记--Zend 自建类的的引用问题

Posted on 2011-02-12 19:32  bobolive  阅读(374)  评论(0编辑  收藏  举报

我在我的项目文件的library目录下,建了如下目录,并加了命名空间"Zwb_"

在Entity文件夹下的user.php内建一个类

Zwb_Models_Entity_User
1 class Zwb_Models_Entity_User {
2 public $id;
3 public $name;
4 public $password;
5 public $content;
6 public $role;
7 /**
8 * @return the $name
9 */
10 public function getName() {
11 return $this->name;
12 }
13
14 /**
15 * @param $name the $name to set
16 */
17 public function setName($name) {
18 $this->name = $name;
19 }
20 /**
21 * @return the $id
22 */
23 public function getId() {
24 return $this->id;
25 }
26
27 /**
28 * @return the $password
29 */
30 public function getPassword() {
31 return $this->password;
32 }
33
34 /**
35 * @return the $content
36 */
37 public function getContent() {
38 return $this->content;
39 }
40
41 /**
42 * @return the $role
43 */
44 public function getRole() {
45 return $this->role;
46 }
47
48 /**
49 * @param $id the $id to set
50 */
51 public function setId($id) {
52 $this->id = $id;
53 }
54
55 /**
56 * @param $password the $password to set
57 */
58 public function setPassword($password) {
59 $this->password = $password;
60 }
61
62 /**
63 * @param $content the $content to set
64 */
65 public function setContent($content) {
66 $this->content = $content;
67 }
68
69 /**
70 * @param $role the $role to set
71 */
72 public function setRole($role) {
73 $this->role = $role;
74 }
75
76
77 }

在EntityIm文件夹下的User.php内建一个类

class Zwb_Models_EntityIm_User {
public function __construct() {
}

public function fetchAll() {
$db = Zwb_Models_DbEntry::getInstance()->getdb();
$users = array();

$result = $db->fetchAll("SELECT * FROM users ");
foreach($result as $row)
{
$user = new Zwb_Models_Entity_user();
$user->setName($row['name']);
$user->setId($row['id']);
$user->setRole($row['role']);
$user->setPassword($row['password']);
$user->setContent($row['content']);
$users[] = $user;
}
return $users;
}
}

我在ListusersController.php里调用它们

public function indexAction() {
$userIm = new Zwb_Models_EntityIm_User();
$users= $userIm->fetchAll();
$this ->_smarty->assign('users', $users);
}

这时候出现php错误:

找得到 Zwb_Models_EntityIm_User 却找不到 Zwb_Models_Entity_User.这是怎么回事呢,我注意到:EntityIm_User是在Controller里面调用的,而Entity_User是在EntityIm_User这个我自建类里面调用的,并且之前在我的windows下测试是没有问题的,传到服务器就出现了这个问题。

我尝试着在EntityIm_User类里面加上这么一 句:

1 require_once 'Zwb/Models/Entity/User.php';

这时个出现错误:Fatal error:  can not open file 'Zwb/Models/Entity/User.php'......

这又是怎么回事呢?我接着试着让它传过去

public function indexAction() {
$userIm = new Zwb_Models_EntityIm_User();
$users = new Zwb_Models_Entity_User();
$users= $userIm->fetchAll($users);
$this ->_smarty->assign('users', $users);
}

这下没有问题了!这时我注释掉//$users = new Zwb_Models_Entity_User(); 并去掉fetchAllj里面的参数。

再次尝试 require_once 'Zwb/Models/Entity/User.php';居然也可以了!这是不稳定吗?

最终我的两个解决方案一:

repuir_once

require_once 'Zwb/Models/Entity/User.php';
class Zwb_Models_EntityIm_User {
}

方案二:

在调用前先建一个这样的类,新建的类名不一定要跟返回的结果一样,可以$users1= new....; $users2 = $userIm->fetchAll();

public function indexAction() {
$userIm = new Zwb_Models_Entity_User();
$users = new Zwb_Models_Entity_User();
$users = $userIm->fetchAll();
$this ->_smarty->assign('users', $users);
}

但我还是不明白为什么在自建类里面不能直接引用。求解。