2016-09-17 45 views
-1

我想創建一個類,加載網站中的任何網站時加載所有的類。在這裏,你有我想要加載類:召喚加載網頁上的所有php類

<?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; 
    } 

注意:類擴展配置(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

你在這裏混合了靜態變量和類變量。

您正嘗試使用$ this設置類的靜態變量,這與類的實例有關。要設置你應該使用自己的::靜態變量,__construct裏面 - 它被調用時,創建類即一個實例:

<?php 
$blah = new Conf(); 

而且,你的第一個CLASSE奇怪的是,當你正在擴大的conf ,而且還在構造函數中創建它的一個新實例。

我認爲將配置文件中定義的所有變量都定義爲常量會更有意義,然後您可以創建一個新的配置類,將這些常量作爲參數傳遞給構造函數,使其能夠重用後來。

的config.inc.php

​​

而且你的類是這樣的:

<?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); 
+0

哇!感謝您的指導。我認爲我需要使用類似的方式來使用Java,我的意思是,使用函數調用它們,而不是將它們稱爲靜態xD –

+0

另外,我認爲如果將它們定義爲全局常量,則可以在加載網頁時訪問它們。我會繼續我的方式,因爲需要擴展來訪問它們。我的意思是,一個受保護的函數是一個需要擴展被召喚的函數。然後從任何不是原始頁面的頁面訪問它,或者擴展原始頁面使其無法召喚。我試圖儘可能保持我的網站最安全:P –

+0

林不知道你的意思是什麼時候網頁加載。 –

0

錯誤在你的代碼,

首先是sta >運營商

  $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"; 

嘗試你可能想看看在spl auto loader,這樣每當類被聲明,它會 - 抽動屬性無法通過以下方式訪問加載文件自動和你不會有自行包括類

<? // scInit.php 
    define("SC_ROOT", dirname(__FILE__) ."/"); 
    define("SC_PREFIX", "sc"); 
    define("SC_EXTENSION", ".php"); 
    spl_autoload_register(
      function ($namespace) { 
       $parts=explode('\\', $namespace); 
       $class_name= end($parts); /// break namespaces and take last part as class name. 

       $fileDirectory = array("model", "view", "controller", "admin", "registry"); // Directories to look for the class file 
       foreach ($fileDirectory as $directory) { 
        $classFileName = SC_ROOT .$directory."/". SC_PREFIX . $class_name . SC_EXTENSION; 
        if (file_exists($classFileName)) { 
         include_once($classFileName); 
        } 
       } 
     }); 

<?php 
//index.php 

namespace project\admin; 
include_once("scInit.php"); 

use project\model\TimeStamp; 

$con= new TimeStamp(); 

/* class TimeStamp is not loaded so now it will run spl_autoloader before generating fatal error passing project\model\TimeStamp as parameter. 
spl_autoloader will look for scTimeStamp.php in all directories defined in autoload function. 

*/ 

我假設你正在嘗試加載mysql的連接憑據。請訪問一次DBCon可能對您有用