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

3

11

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

Dunnage answered 1/7, 2018 at 19:31 Comment(6)
In the same file "initialisation_site.php" I have string variables and class objects. Eclipse can "see" the class but not the string variables. Why ????Leicester
Related: bugs.eclipse.org/bugs/show_bug.cgi?id=538418Siegfried
$Modeles_Nom: Where is this variable defined?Swami
It looks like $Modeles_Nom is he undefined a. Pretty obvious actually!Crossjack
@Crossjack I know that he made a mistake to place that undefined variable there but the problem is about Eclipse PHP Variable Validator ignores include and require.Siegfried
The point is STILL that «PHP Variable Validator ignores include and require», as @Siegfried said.Erotogenic
S
5

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");
Saddlecloth answered 5/9, 2018 at 18:35 Comment(0)
D
4

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.

Delphiadelphic answered 26/10, 2019 at 17:11 Comment(2)
This is also useful when a file is included somewhere else, and the variable is defined before the include.Symbolics
I am using the CodeIgniter MVC framework, and this Eclipse "undefined" warning comes up everywhere. I tried GLOBAL, which worked in some places but caused issues in others. This @var solution worked in all cases.Gastelum
S
3

This probably means that Eclipse does not understand that those variables are defined in the file that you import with require_once.

Maybe it is possible to switch the annotation for undefined variables off in the Eclipse settings? But then you'd also not see warnings for other undefined variables.

Seger answered 1/7, 2018 at 19:40 Comment(2)
This looks more like a comment than any solution.Mitzimitzie
That means that all my PHP includes or requires will not be seen as part of the php file where they included ? I am sure Eclipse can do betterLeicester

© 2022 - 2024 — McMap. All rights reserved.