Include file gives undefined variable warning

General discussion on Zend Studio

Re: Include file gives undefined variable warning

Postby fonant on Thu Oct 06, 2011 6:27 pm

Drupal's templating system causes lots of "Undefined variable" issues.

What would be _really_ nice is to have a way to "define" variables in PHP comments, perhaps something like:

Code: Select all
<?php
/*
 * Variables available to this file:
 *  @defined $picture
 *  @defined $title
 */
?>

<h2><?php echo $title; ?></h2>
<div class="picture"><?php echo $picture; ?></div>
fonant
 
Posts: 50
Joined: Thu Mar 05, 2009 2:56 pm
Location: Lancing, West Sussex, UK

Re: Include file gives undefined variable warning

Postby crazymerlin on Sat Oct 08, 2011 9:39 pm

It would seem to me that a point is being missed:

An included file should only contain data that is optional. If the data in the file is not optional, then the file needs to be required, not included.
So to test whether the issue is in the code analyzer, change the include for a required.

If you are using an include, then you should check for the existence of the variable before using it as an include can fail to load without a fatal error...which is the correct behavior.
If you 'require' the file and the code analyzer still fails, then take a look at the preferences for that feature (the analyzer).
crazymerlin
 
Posts: 243
Joined: Wed Feb 18, 2009 10:25 pm
Location: Beaverton, OR, USA

Re: Include file gives undefined variable warning

Postby fonant on Tue Feb 12, 2013 10:30 pm

@craxymerlin

No, you mis-understand. The included file is using variables defined by the file that is including it, not the other way round.

There's no way for Studio to know where the file might be included, so it always marks the variables (which are set before the file is included) as being undefined. What we need is a way to tell Studio about these variables (that are defined before the file is included), so we can avoid lots of "undefined variable" warnings.

This is how the Drupal template system works, vastly simplified. First you have PHP code that sets values for the variables to be inserted into the HTML template:

Code: Select all
<?php
$page_title = "Page title";
$page_content = "<p>Lorem Ipsum dolorem est...</p>";
$template_name = 'page';
include "$template_name.tpl.php";


Then you have a template file that has the HTML and the variables in it:

Code: Select all
<html>
<h1><?php echo $page_title; ?></h1>
<?php echo $page_content; ?>
</html>


The template file generates lots of "undefined variables" warnings, because as far as Studio is concerned $page_title, $page_content, aren't defined (when in fact they are).
fonant
 
Posts: 50
Joined: Thu Mar 05, 2009 2:56 pm
Location: Lancing, West Sussex, UK

Re: Include file gives undefined variable warning

Postby strgt on Tue Aug 05, 2014 10:52 pm

withoud getting involved in "how you should code". I think the 2 options are

Code: Select all
<?php
   /**
    * This file is being included, so, variables are being set outside
    *
    * @var $title
    * @var $picture
    */
?>
<h2><?php echo $title; ?></h2>
<div class="picture"><?php echo $picture; ?></div>

or

Code: Select all
<?php
   /**
    * This file is being included, so, variables are being set outside
    */
/* @var $title string */
/*@var $picture string*/
?>
<h2><?php echo $title; ?></h2>
<div class="picture"><?php echo $picture; ?></div>
strgt
 
Posts: 35
Joined: Sat Feb 21, 2009 1:50 pm

Re: Include file gives undefined variable warning

Postby strgt on Thu May 18, 2017 11:02 pm

UPDATE! /* @var $youtExternalDefinedVar string */ Does work now!
strgt
 
Posts: 35
Joined: Sat Feb 21, 2009 1:50 pm

Re: Include file gives undefined variable warning

Postby judy6640 on Sat Jun 17, 2017 11:09 am

Hello. I also had this problem. Thank you
judy6640
 
Posts: 9
Joined: Sat Jun 17, 2017 10:07 am

Previous

Return to Zend Studio

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron