我需要看看你的班级成员。
但是当您尝试使用未定义的对象变量时会引发此类错误。
您要么需要使用 setter 将 $conf 传递给 Member 类,如下所示:
需要'config.php';
$member = new Member();
$member->setConfig($conf);
在内部,您的 setter 和类可能如下所示:
...
/** var array $config **/
private $config = array();
public function setConfig(array $conf)
{
$this->config = $conf;
}
public function getPort()
{
return $this->config['port];
}
...
现在,我个人更喜欢只使用类并避免使用数组()。如果其他人结束使用您的类,则必须了解数组结构而不是类定义。
快速重新实现您要实现的目标,如下所示:
...
php INI File:
[database]
username = root
password = 1234
database = localhost
host = localhost
// Immutable value Object
Class Config
{
private $username;
private $password;
private $host;
private $database;
private $port;
private $prefix;
public function __construct($username, $password, $host, $database, $port, $prefix = 'db_')
{
$this->username = $username;
$this->password = $password;
$this->host = $host;
$this->database = $database;
$this->port = $port;
$this->prefix = $prefix;
}
public function getUsername()
{
return $this->username;
}
/**
* @return mixed
*/
public function getDatabase()
{
return $this->database;
}
/**
* @return mixed
*/
public function getHost()
{
return $this->host;
}
/**
* @return mixed
*/
public function getPassword()
{
return $this->password;
}
/**
* @return mixed
*/
public function getPort()
{
return $this->port;
}
/**
* @return mixed
*/
public function getPrefix()
{
return $this->prefix;
}
}
// Entity
class Member
{
private $name;
private $password;
private $email;
private $config;
public function setPost($post)
{
$this->name = isset($post->name) ? $post->name : 'admin';
$this->email = isset($post->email) ? $post->email : 'admin@localhost';
$this->password = isset($post->password) ? $post->password : '1234';
}
/**
* @param mixed $config
*/
public function setConfig($config)
{
$this->config = $config;
}
/**
* @return mixed
*/
public function getConfig()
{
return $this->config;
}
/**
* @param mixed $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* @return mixed
*/
public function getPassword()
{
return $this->password;
}
}
class Connection
{
/**
* @var Config $config
*/
private $config;
private $connection;
public function __construct(Config $config)
{
$this->config = $config;
}
public function connect()
{
if (!$this->connection instanceof \PDO) {
$this->connection = new \PDO(
'mysql:database=' . $this->config->getDatabase() .
';host=' . $this->config->getHost() .
';port=' . $this->config->getPort() . ';',
$this->config->getUsername(),
$this->config->getPassword()
);
} else {
$this->connection;
}
}
}
...
那么我们现在应该有:
定义('ROOTPATH',目录。'/');定义('CLASSES',DIR .'/classes/');
//Lazy load any required php classes
function __autoload($class_name) {
$file = CLASSES . $class_name . '.php';
if( file_exists($file) ) {
require_once $file;
} else {
echo "Can't find ". $file;;
}
}
//does config exist?
if (!file_exists('config.ini')) {
//redirect to install routine if config does not exist
header(ROOTPATH . 'admin/install.php') ;
}
if(false == $config = parse_ini_file('config.ini')) {
//Do something else
}
$post = (object)$_POST;
$configClass = new Config(
$config['username'],
$config['password'],
$config['host'],
$config['database'],
$config['port']
);
$connection = new Connection($configClass);
try {
$connection->connect();
} catch(\Exception $e) {
die($e->getMessage());
}
$member = new Member();
//Either
$member->setPost($post);
//OR
$member->setName($post->name);
$member->setEmail($post->email);
$member->setPassword($post->password);
//I do not understand, what you are try to do below but its from your question
$member->updateDatabse();
...
我不确定你想用你的成员类实现什么,它是活动记录吗?
不要使用全球,它的陷阱。