我是否知道如何在CodeIgniter中传输Object。以下方式是否正确?
class UserLibrary{
private $username;
public getUsername() {
return this->username;
}
public void setUsername(username) {
this->username = username;
}
}
//设置值
$Reg = new UserLibrary;
$Reg->setName(name);
//如何获取价值?
答案 0 :(得分:1)
您的PHP代码存在严重问题。
与CodeIgniter一样,您的库类应放在 application / libraries 文件夹中,因为CodeIgniter在初始化时会查找它们。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Userlibrary {
private $username;
public function getUsername() {
return $this->username;
}
public function setUsername($username) {
$this->username = $username;
}
}
/* End of file Userlibrary.php */
要加载您的库,您应该将其加载到控制器中:
$this->load->library('userlibrary');
然后您可以使用您的库:
$this->userlibrary->setUsername('your username');
echo $this->userlibrary->getUsername();
有关更多信息,请使用官方文档:http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html