我正在尝试创建一个类,当Web中的任何站点加载时,该类加载所有类。这里有你要加载的类:
<?php
include_once filter_input(INPUT_SERVER, "DOCUMENT_ROOT") . "/Config.inc.php";
class Connection extends Conf{
private static $connection;
private static $config;
public function __construct(){
$this->config = new Conf;
}
注意:该类扩展了Conf(config.inc.php文件),因为获取数据的方法受到保护,而不是公共。
我还要加载以下类:
<?php
/*
*
* El contenido de este archivo está protegido y para incluirse debe extenderse a la clase Config
*
*/
Class Conf {
private static $db_server = "server";
private static $db_user = "username";
private static $db_pass = "password";
private static $db_maindb = "database";
private static $server_folder = "folder";
public function __construct(){
$this->db_server = "server";
$this->db_user = "username";
$this->db_pass = "password";
$this->db_maindb = "database";
$this->server_folder = "folder";
}
我确信它是这样的:
class Builder {
public function build(){
Connection::__construct();
Conf::__construct();
}
}
但它不起作用。请帮忙吗?
答案 0 :(得分:0)
你在这里混合静态变量和类变量。
您正在尝试使用$ this在类上设置静态变量,该变量与类的实例相关。要设置静态变量,你应该使用self ::,在__construct里面 - 当你创建一个类的实例时会调用它,即:
<?php
$blah = new Conf();
此外,您的第一个classe很奇怪,因为您正在扩展Conf,但也在构造函数中创建它的新实例。
我认为将配置文件中定义的所有变量作为常量更有意义,然后您可以创建一个新的配置类,将这些常量作为参数传递给构造函数,使其能够在以后重用
的config.inc.php
<?php
define("SERVER", "hostname");
define("USERNAME", "username");
define("PASSWORD", "P@ssw0rd");
define("DATABASE", "maindb");
你的课程是这样的:
<?php
include_once filter_input(INPUT_SERVER, "DOCUMENT_ROOT") . "/Config.inc.php";
class Connection
{
private $connection;
private $config;
public function __construct(Config $config)
{
$this->config = $config;
}
public function connect()
{
$this->connection = new mysqli($this->config->getServer(), $this->config->getUsername(), $this->config->getPassword(), $this->config->getDatabase());
}
}
class Config
{
private $server;
private $username;
private $password;
private $database;
/**
* @return mixed
*/
public function getServer()
{
return $this->server;
}
/**
* @param mixed $server
*/
public function setServer($server)
{
$this->server = $server;
}
/**
* @return mixed
*/
public function getUsername()
{
return $this->username;
}
/**
* @param mixed $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* @return mixed
*/
public function getPassword()
{
return $this->password;
}
/**
* @param mixed $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* @return mixed
*/
public function getDatabase()
{
return $this->database;
}
/**
* @param mixed $database
*/
public function setDatabase($database)
{
$this->database = $database;
}
/**
* @return string
*/
public function getServerFolder()
{
return $this->server_folder;
}
/**
* @param string $server_folder
*/
public function setServerFolder($server_folder)
{
$this->server_folder = $server_folder;
}
private $server_folder;
public function __construct($server, $username, $password, $database)
{
$this->server = $server;
$this->username = $username;
$this->password = $password;
$this->database = $database;
$this->server_folder = "folder";
}
}
然后,最后使用它:
<?php
$config = new Config($server, $username, $password, $database);
$connection = new Connection($config);
答案 1 :(得分:0)
代码出错,
首先无法通过 - &gt;访问静态属性运营商
line-height
尝试使用self :: for static members来工作
text
您可能希望查看spl auto loader,以便无论何时声明类,它都会自动加载文件而您不必自己包含类
$this->db_server = "server";
$this->db_user = "username";
$this->db_pass = "password";
$this->db_maindb = "database";
$this->server_folder = "folder";
和
self::$db_server = "server";
self::$db_user = "username";
self::$db_pass = "password";
self::$db_maindb = "database";
self::$server_folder = "folder";
我假设您正在尝试加载mysql的连接凭据。请访问DBCon,这可能对您有用