Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse Php : variable undefined evenif it is defined in an require_once php file

I am using as a beginner : Eclipse IDE for PHP Developers Version: Photon Release (4.8.0) Build id: 20180619-1200 OS: Windows 10, v.10.0, x86_64 / win32 Java version: 1.8.0_77

I have a file index.php with a require_once(initialisation.php) The initialisation.php defines a variable $Modeles_Chemin And the variable $Modeles_Chemin is used in index.php (after the require_once)

On my website, it works fine no undefined variable but in eclipse editor I receive an undefined variable.

(Of course I have the same problem with the other variables).

Here is an extract : index.php :

<?php
require_once("prog/php/initialisation_site.php");
include($Modeles_Chemin.$Modeles_Nom."/html_begin.php");

initialisation_site.php :

        $Contenu_Chemin = "contenu/";
$Modeles_Chemin = $Contenu_Chemin."modeles/";

How can I fix this ?

thanks

like image 544
Stéphane BERTHO Avatar asked Jul 01 '18 19:07

Stéphane BERTHO


2 Answers

You can use global:

require_once("prog/php/initialisation_site.php");

global $Modeles_Chemin;
global $Modeles_Nom;

include($Modeles_Chemin.$Modeles_Nom."/html_begin.php");
like image 188
Pipe Avatar answered Sep 21 '22 09:09

Pipe


If you want to get rid of these warnings you may consider putting this kind of comment on the top of the file:

/** @var Type $variable */

This is a workaround but I find it to be a good approach. This kind of a comment is letting me know that I am using a variable that is defined in a different file.

like image 25
Marcin Avatar answered Sep 18 '22 09:09

Marcin