-2

Hi i have a php error that it hought you might be able to help with Cannot redeclare class I have researched many results but none apply or help me with my issue I have used the once of requiring/including but it still errors

And it sometimes says Notice: Undefined index: lol in C:\xampp\htdocs\template.php that is weird also because i dont understand why its like that.

The index

<?php
require_once 'settings.php';
require_once 'global.php';
if (!isset($_GET['url']))
{
    include("pages/index.php");
}
if (file_exists("".$hotelurl."/pages/".$_GET['url'].".php"))
{
    include("pages/".$_GET['url'].".php");
}
?>

My global.php

<?php
require_once 'template.php';
use Central as C;
$template = new C\template();
$template->Initiate();
?>

My settings.php

<?php
$hotelurl = "http://127.0.0.1";
?>

My template.php

<?php
class template

{

public $tpl;

private $params = array();

final public function Initiate()
{
    $this->setParams('lol', 'centraltest');
}

final public function setParams($key, $value)
{   
    $this->params[$key] .= $value; 
}

final public function filterParams($str)
{
    foreach($this->params as $key => $value)
    {
        $str = str_ireplace('{' . $key . '}', $value, $str);
    }

    return $str;
}

final public function write($str)
{
    $this->tpl .= $str;
}

final public function outputTPL()
{
    echo $this->filterParams($this->tpl);
    unset($this->tpl);
}
    }
    ?>
user3354197
  • 95
  • 2
  • 11
  • 4
    **Cannot redeclare class** i don't see how that can be clearer –  Mar 03 '14 at 21:19
  • Because there are no sighns of double classing – user3354197 Mar 03 '14 at 21:20
  • you're probably loading your `template.php` file via a require() or include() somewhere, instead of using the _once() versions. SO you'll be loading the template file TWICE, causing PHP to see you defining that class TWICE. – Marc B Mar 03 '14 at 21:20
  • I think your period key is broken. Your question contains no more than one, which makes it pretty much unreadable. – GolezTrol Mar 03 '14 at 21:20
  • Your index error is because you're trying to concatenate to an undefined index 'lol' here: `$this->params[$key] .= $value;`. – phpisuber01 Mar 03 '14 at 21:23
  • `require_once` does see to it that a file does not get embedded twice within a script – if you’re getting _Cannot redeclare class template_ nonetheless, then you either have embedded it somewhere else not using `_once`, or you have another file with a class with the same name in it embedded somewhere. As for the other thing – some _research_ before asking, please. http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – CBroe Mar 03 '14 at 21:24

1 Answers1

0
Cannot redeclare class and Undefined index

means that you are creating a class with a name that is already taken (for example by the standard class names).

Solution: rename it

possible scenario as well - good comment:

you're probably loading your template.php file via a require() or include() somewhere, instead of using the _once() versions. SO you'll be loading the template file TWICE, causing PHP to see you defining that class TWICE. – Marc B

Notice: Undefined index: lol in C:\xampp\htdocs\template.php

means that you are trying to access an index (in your case index 'lol') that is not set yet (you are using .= which is reading and writing).

solution: check if it is already been taken or ignore it (its 'just' a notice). You can use either the language construct isset or the function array_key_exists.

Langusten Gustel
  • 10,917
  • 9
  • 46
  • 59