PHP Warning: Undefined array key bij upgrade php 8.0 of hoger

Status
Niet open voor verdere reacties.

Martijn31

Gebruiker
Lid geworden
6 sep 2009
Berichten
362
Hallo Helpmij,

ik wil graag met de tijd mee en overgaan op php 8.2. Helaas krijg ik wanneer ik mijn versie naar php 8.0 of hoger ga de volgende errors:

Code:
[Wed Apr 19 20:47:02.393148 2023] [lsapi:warn] [pid 3511592:tid 139840845721120] [client 99.157.198.157:50257] [host mijndomein.nl] Backend log: PHP Warning:  Undefined array key "url" in /home/mijnwebhostnaam/domains/mijndomein.nl/public_html/app/class.template.php on line 57\n
[Wed Apr 19 20:47:02.399411 2023] [lsapi:warn] [pid 3511592:tid 139840845721120] [client 99.157.198.157:50257] [host mijndomein.nl] Backend log: PHP Warning:  Undefined array key "online" in /home/mijnwebhostnaam/domains/mijndomein.nl/public_html/app/class.template.php on line 57\n
[Wed Apr 19 20:47:02.405423 2023] [lsapi:warn] [pid 3511592:tid 139840845721120] [client 99.157.198.157:50257] [host mijndomein.nl] Backend log: PHP Warning:  Undefined array key "status" in /home/mijnwebhostnaam/domains/mijndomein.nl/public_html/app/class.template.php on line 57\n
[Wed Apr 19 20:47:02.405563 2023] [lsapi:warn] [pid 3511592:tid 139840845721120] [client 99.157.198.157:50257] [host mijndomein.nl] Backend log: PHP Warning:  Undefined array key "skin" in /home/mijnwebhostnaam/domains/mijndomein.nl/public_html/app/class.template.php on line 57\n
[Wed Apr 19 20:47:02.405570 2023] [lsapi:warn] [pid 3511592:tid 139840845721120] [client 99.157.198.157:50257] [host mijndomein.nl] Backend log: PHP Warning:  Undefined array key "username" in /home/mijnwebhostnaam/domains/mijndomein.nl/public_html/app/class.template.php on line 57\n/home/mijnwebhostnaam/domains/mijndomein.nl/public_html/app/class.template.php on line 57\n
[Wed Apr 19 20:47:02.405602 2023] [lsapi:warn] [pid 3511592:tid 139840845721120] [client 99.157.198.157:50257] [host mijndomein.nl] Backend log: PHP Warning:  Undefined array key "email" in/home/mijnwebhostnaam/domains/mijndomein.nl/public_html/app/class.template.php on line 57\n
[Wed Apr 19 20:47:02.411573 2023] [lsapi:warn] [pid 3511592:tid 139840845721120] [client 99.157.198.157:50257] [host mijndomein.nl] Backend log: PHP Warning:  Undefined array key "newsTitle-1" in /home/mijnwebhostnaam/domains/mijndomein.nl/public_html/app/class.template.php on line 57\n
[Wed Apr 19 20:47:02.411731 2023] [lsapi:warn] [pid 3511592:tid 139840845721120] [client 99.157.198.157:50257] [host mijndomein.nl] Backend log: PHP Warning:  Undefined array key "newsID-1" in /home/mijnwebhostnaam/domains/mijndomein.nl/public_html/app/class.template.php on line 57\n
[Wed Apr 19 20:47:02.411764 2023] [lsapi:warn] [pid 3511592:tid 139840845721120] [client 99.157.198.157:50257] [host mijndomein.nl] Backend log: PHP Warning:  Undefined array key "newsDate-1" in /home/mijnwebhostnaam/domains/mijndomein.nl/public_html/app/class.template.php on line 57\n
[Wed Apr 19 20:47:02.411774 2023] [lsapi:warn] [pid 3511592:tid 139840845721120] [client 99.157.198.157:50257] [host mijndomein.nl] Backend log: PHP Warning:  Undefined array key "newsCaption-1" in /home/mijnwebhostnaam/domains/mijndomein.nl/public_html/app/class.template.php on line 57\n
[Wed Apr 19 20:47:02.411800 2023] [lsapi:warn] [pid 3511592:tid 139840845721120] [client 99.157.198.157:50257] [host mijndomein.nl] Backend log: PHP Warning:  Undefined array key "newsIMG-1" in /home/mijnwebhostnaam/domains/mijndomein.nl/public_html/app/class.template.php on line 57\n
[Wed Apr 19 20:47:02.411812 2023] [lsapi:warn] [pid 3511592:tid 139840845721120] [client 99.157.198.157:50257] [host mijndomein.nl] Backend log: PHP Warning:  Undefined array key "newsTitle-2" in

in php 7.4 ervaar ik geen problemen. de class.template.php waar naartoe wordt verwezen:
PHP:
<?php

namespace Revolution;
if(!defined('IN_INDEX')) { die('Sorry, you cannot access this file.'); }
class template implements iTemplate
{

	public $tpl;
	
	private $params = array();

	final public function Initiate()
	{
		global $_CONFIG, $users, $engine, $core, $template;
		$this->setParams('Name', $_CONFIG['soccer']['name']);
		$this->setParams('Desc', $_CONFIG['soccer']['desc']);
		$this->setParams('url', $_CONFIG['soccer']['url']);
		$this->setParams('online', $core->getOnline());
		$this->setParams('status', $core->getStatus());
		$this->setParams('server_ip', $_CONFIG['soccer']['server_ip']);
		$this->setParams('mysql_host', $_CONFIG['mysql']['hostname']);
		$this->setParams('mysql_port', $_CONFIG['mysql']['port']);
		
		$this->setParams('skin', $_CONFIG['template']['style']);
		
		if($users->isLogged())
		{	
			$this->setParams('username', $users->getInfo($_SESSION['user']['id'], 'username'));
			$this->setParams('rank', $users->getInfo($_SESSION['user']['id'], 'rank'));
			$this->setParams('motto', $users->getInfo($_SESSION['user']['id'], 'motto'));
			$this->setParams('email', $users->getInfo($_SESSION['user']['id'], 'mail'));

			if($this->params['rank'] > 3)
			{
				$this->setParams('editor', '<li><a href="ase/">editor</a></li>'); 
			}
			else
			{
				$this->setParams('editor', ''); 
			}
			
			if($_GET['url'] == 'me' || $_GET['url'] == 'account' || $_GET['url'] == 'home' || $_GET['url'] == 'settings' || $_GET['url'] == 'info')
			{
				$template->form->getPageHome();				
			}
			
			if($_GET['url'] == 'news' || $_GET['url'] == 'articles')
			{
				$template->form->getPageNews();
			}		
		}
		
	}
	
	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);
	}
}
?>

weet iemand waarom dit nu voor problemen zorgt en of dit simpel is om op te lossen?
alvast bedankt!
 
Moet het geen string zijn?

$this->params['$key'] .= $value;
 
Hallo Aar,

Ik heb het aangepast in de code, helaas werkt dit niet. ik vraag me ook af waarom in PHP 7.4 het geen problemen geeft maar vanaf PHP 8 of hoger wel
 
Hallo Aar,

Ik heb het aangepast in de code, helaas werkt dit niet. ik vraag me ook af waarom in PHP 7.4 het geen problemen geeft maar vanaf PHP 8 of hoger wel
Omdat het een nieuwe versie is?
 
Wat komt er uit een print_r() met

$_CONFIG
 
wanneer ik print_r($_CONFIG); doe komen alle array's zichtbaar op de pagina. maar ook database gegevens etc. array $key zit er niet tussen. $_config verwijst naar mijn configuratie bestand. $key moet eigenlijk de array's uit mijn configuratiebestand ophalen.
 
Weet je het type van $key in function setParams?

PHP:
echo gettype($key), "\n";
$this->params[$key] .= $value;

aanvulling. Je zou private even op public kunnen zetten (regel 10).
 
Laatst bewerkt:
Hallo bron,

Dank voor je antwoord. Wanneer ik de code toepas komt er "string" te staan. Vervolgens geeft hij dezelfde foutweergave als in mijn eerste bericht. Van private naar public gaan geeft geen (zichtbaar) effect.
 
En wat gebeurt er met

PHP:
public $params = array();


of als het array alvast wordt voorbereid met een lege string (als workaround)

PHP:
private $params = array(
    "Name" => "",
    "Desc" => "",
    "url" => "",
    "online" => "",
    "status" => "",
    "server_ip" => "",
    "mysql_host" => "",
    "mysql_port" => "",
    "skin" => "",
    "username" => "",
    "rank" => "",
    "motto" => "",
    "email" => "",
    "editor" => "",
    "newsTitle-1" => "",
    "newsID-1" => "",
    "newsDate-1" => "",
    "newsCaption-1" => "",
    "newsIMG-1" => "",
    "newsTitle-2" => "",
);
 
Laatst bewerkt:
nu wat hier het probleem is dat dit niet meer toegelaten actie is..

dus moet je eerste een sleutel maken met

$x[$y] = 'veel tekst';

vervolgens kan je wel

$x[$y] .= 'veel tekst';

gebruiken

dus zou ik zeggen gebruik eerst

if(key_exist($uwy,$uwarrayx)){
$x[$y] = 'veel tekst';
}else{
$x[$y] .= 'veel tekst';
}


PHP:
<?php
namespace Revolution;
if(!defined('IN_INDEX')) { die('Sorry, you cannot access this file.'); }
class template implements iTemplate{
     public $tpl;
     private $params = array();
     final public function Initiate()  {
        global $_CONFIG, $users, $engine, $core, $template;
        $this->setParams('Name', $_CONFIG['soccer']['name']);
        $this->setParams('Desc', $_CONFIG['soccer']['desc']);
        $this->setParams('url', $_CONFIG['soccer']['url']);
        $this->setParams('online', $core->getOnline());
        $this->setParams('status', $core->getStatus());
        $this->setParams('server_ip', $_CONFIG['soccer']['server_ip']);
        $this->setParams('mysql_host', $_CONFIG['mysql']['hostname']);
        $this->setParams('mysql_port', $_CONFIG['mysql']['port']);
        $this->setParams('skin', $_CONFIG['template']['style']);
        if($users->isLogged())  {   
            $this->setParams('username', $users->getInfo($_SESSION['user']['id'], 'username'));
            $this->setParams('rank', $users->getInfo($_SESSION['user']['id'], 'rank'));
            $this->setParams('motto', $users->getInfo($_SESSION['user']['id'], 'motto'));
            $this->setParams('email', $users->getInfo($_SESSION['user']['id'], 'mail'));
 
            if($this->params['rank'] > 3)  {          $this->setParams('editor', '<li><a href="ase/">editor</a></li>'); 
            }  else     {                                       $this->setParams('editor', '');             }
            if($_GET['url'] == 'me' || $_GET['url'] == 'account' || $_GET['url'] == 'home' || $_GET['url'] == 'settings' || $_GET['url'] == 'info')  {
                $template->form->getPageHome();             
            }
            if($_GET['url'] == 'news' || $_GET['url'] == 'articles') {
                $template->form->getPageNews();
            }       
        }
    }
    final public function setParams($key, $value)  {// die .= is toch wel af te raden bij  set methodes omdat het voor ander programmeurs mogelijk fout geinterpreteert zal worden
       if(key_exist($key,  $this->params)){
           $this->params[$key] .= $value;  
      }else{
          $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);
    }
}
?>

DIE FINAL ZAL MOGELIJKS HET VOLGENDE ZIJN
 
Laatst bewerkt:
Hallo allemaal!

Dank voor jullie reactie, kenikavanbis jou idee geeft alleen maar meer errors helaas. bron heeft het helpende antwoord gegeven. het voorbereiden van een array met een lege string is voldoende om de waarschuwing weg te krijgen.
Code:
private $params = array(
    "Name" => "",
    "Desc" => "",
    "url" => "",
    "online" => "",
    "status" => "",
    "server_ip" => "",
    "mysql_host" => "",
    "mysql_port" => "",
    "skin" => "",
    "username" => "",
    "rank" => "",
    "motto" => "",
    "email" => "",
    "editor" => "",
    "newsTitle-1" => "",
    "newsID-1" => "",
    "newsDate-1" => "",
    "newsCaption-1" => "",
    "newsIMG-1" => "",
    "newsTitle-2" => "",
);
bron, bedankt!:d
 
Graag gedaan. Suc6 met de migratie naar php 8.2 :thumb:
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan