can’t access already instantiated class data

Asked

Viewed 275 times

0

Hello, I am developing a panel and I am developing the instation system and in the part of creating DB, I am not able to access variable of the instacidada class.

Error that appears on the screen:

Fatal error: Cannot redeclare class Op_config

Code:

<?php 
include('../config/config.php');

// Inclui o arquivo de conexão com o código descrito anteriormente
include('../sql/class-connection.php');

// Nosso novo banco de dados
$bd = $_POST['db_name'];

$op_config = new OP_Config();
$bd_user = $op_config->datesDb->config['db_user'];

// Cria o banco de dados e da permissão para nosso usuário no mesmo
$db = new DB();
$verifica = $db->getConnection->conn->exec(
    "CREATE DATABASE IF NOT EXISTS `$bd`;
    GRANT ALL ON `$bd`.* TO '$bd_user'@'localhost';
    FLUSH PRIVILEGES;"
);

// Verificamos se a base de dados foi criada com sucesso
if ( $verifica ) {
    echo 'Banco de dados criado com sucesso!';
} else {
    echo 'Falha ao criar banco de dados!';
}
?>
  • 1

    You can use include_once or require_once, or implement a autoload

3 answers

1

Use the include_once, in that way:

<?php 
include_once('../config/config.php');

// Inclui o arquivo de conexão com o código descrito anteriormente
include_once('../sql/class-connection.php');

// Nosso novo banco de dados
$bd = $_POST['db_name'];

$op_config = new OP_Config();
$bd_user = $op_config->datesDb->config['db_user'];

// Cria o banco de dados e da permissão para nosso usuário no mesmo
$db = new DB();
$verifica = $db->getConnection->conn->exec(
    "CREATE DATABASE IF NOT EXISTS `$bd`;
    GRANT ALL ON `$bd`.* TO '$bd_user'@'localhost';
    FLUSH PRIVILEGES;"
);

// Verificamos se a base de dados foi criada com sucesso
if ( $verifica ) {
    echo 'Banco de dados criado com sucesso!';
} else {
    echo 'Falha ao criar banco de dados!';
}
?>

0

Make sure you are not including 2 times the same class. It may be being included within some other include. For example:

config.php

include ('OP_config.php');

class Config 
{
...
}

class-Connection.php

include('OP_config.php');

class ClassConnection 
{
...
}

It may be that inside the files .php is calling the same class twice or more.

0

That mistake Cannot redeclare class means you are declaring the class more than once, check if in your 2 includes you are not defining the class OP_Config more than once.

  • i just instated it, in createdb . php and in the connection class only that in the connection class the name of the object variable put another!

  • I’m not talking about instantiating, I’m talking about declaring class more than once. Copy other include codes into your question and hide information that could compromise system security.

  • http://pastebin.com/cbn3PyCH is, there the other two archives!

  • You’re making include('../config/config.php'); twice, once inside the class-connection.php and another in your current file.

  • Remove from your current file and try again.

  • but, if I remove and the urge that is used ? will give error!

  • thanks for the help seen here I’ll have to come up with another logic for this system because this maniac will not work!

  • @xCoder You removing the current file will still have the include inside the class-connection.php and you can instantiate without problems.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.