Mario_01 Mario_01 - 2 months ago 8
PHP Question

Summon all php classes on load web page

I'm trying to create a class that loads all the classes when any site in the web loads. Here you have the classes that I want to load:

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


Note: That class extends Conf (config.inc.php file) because the method that gets the data is protected, not public.

And also I want to load the following class:

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

}


I'm sure that it's something like this:

class Builder {
public function build(){
Connection::__construct();
Conf::__construct();
}
}


But it doesn't work. Some help, please?

Answer

You're mixing static variables and class variables here.

You are trying to set static variables on the class using $this, which relates to the instance of the class. To set the static variables you should use self::,Inside the __construct - which gets called when you create an instance of the class i.e:

<?php
$blah = new Conf();

Also, your first classe is strange, as you're extending Conf, but also creating a new instance of it in the constructor.

I think it would make more sense to have all of your variables defined in the config file as constants, and then you could create a new config class passing those constants in as parameters to the constructor, making it able to be reused later.

Config.inc.php

<?php
define("SERVER", "hostname");
define("USERNAME", "username");
define("PASSWORD", "P@ssw0rd");
define("DATABASE", "maindb");

And your classes like this:

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

        }
    }

And then, finally to use it:

<?php

    $config = new Config($server, $username, $password, $database);

    $connection = new Connection($config);
Comments