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

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 ???? Commented Jul 4, 2018 at 21:02
  • 1
    Related: bugs.eclipse.org/bugs/show_bug.cgi?id=538418 Commented Aug 31, 2018 at 7:27
  • 2
    $Modeles_Nom: Where is this variable defined? Commented Sep 4, 2018 at 15:49
  • It looks like $Modeles_Nom is he undefined a. Pretty obvious actually! Commented Sep 5, 2018 at 12:00
  • 1
    @delboy1978uk 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. Commented Sep 5, 2018 at 19:07

3 Answers 3

6
+50

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");
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

This is also useful when a file is included somewhere else, and the variable is defined before the include.
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.
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.

2 Comments

This looks more like a comment than any solution.
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 better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.