php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #62788 newt example error. (easy 1-line fix)
Submitted: 2012-08-09 20:04 UTC Modified: 2012-08-10 06:33 UTC
From: kevdevsandom at yahoo dot co dot uk Assigned:
Status: Not a bug Package: newt (PECL)
PHP Version: Irrelevant OS: NA
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
15 + 29 = ?
Subscribe to this entry?

 
 [2012-08-09 20:04 UTC] kevdevsandom at yahoo dot co dot uk
Description:
------------
---
From manual page: http://www.php.net/newt.examples-usage
---

Line 13 needs to be:
newt_get_screen_size (&$rows, &$cols);

Instead of:
newt_get_screen_size ($rows, $cols);



More info:
This is because those variables aren't created until that point, so without the anpersands it's trying to reference rather than create them and you get this:

PHP Notice:  Undefined variable: rows in /home/ksandom/files/develop/mass-notready/experimentation/newt/newtExample.php on line 13
PHP Notice:  Undefined variable: cols in /home/ksandom/files/devel┌──────┐notready/experimentation/newt/newtExample.php on line 13

The complete code that works for me is included in the test script section.

Test script:
---------------
<?php
newt_init ();
newt_cls ();

newt_draw_root_text (0, 0, "Test Mode Setup Utility 1.12");
newt_push_help_line (null);
newt_draw_root_text (-30, 0, "(c) 1999-2002 RedHat, Inc");

newt_get_screen_size (&$rows, &$cols);

newt_open_window ($rows/2-17, $cols/2-10, 34, 17, "Choose a Tool");

$form = newt_form ();

$list = newt_listbox (3, 2, 10);

foreach (array (
    "Authentication configuration",
    "Firewall configuration",
    "Mouse configuration",
    "Network configuration",
    "Printer configuration",
    "System services") as $l_item)
{
    newt_listbox_add_entry ($list, $l_item, $l_item);
}

$b1 = newt_button (5, 12, "Run Tool");
$b2 = newt_button (21, 12, "Quit");

newt_form_add_component ($form, $list);
newt_form_add_components ($form, array($b1, $b2));

newt_refresh ();
newt_run_form ($form);

newt_pop_window ();
newt_pop_help_line ();
newt_finished ();
newt_form_destroy ($form);
?>

Expected result:
----------------
Nice text interface.

Actual result:
--------------
These errors spawled over the interface, and only the quit button shows:

PHP Notice:  Undefined variable: rows in /home/ksandom/files/develop/mass-notready/experimentation/newt/newtExample.php on line 13
PHP Notice:  Undefined variable: cols in /home/ksandom/files/devel┌──────┐notready/experimentation/newt/newtExample.php on line 13


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-10 06:33 UTC] aharvey@php.net
It's fine the way it is: call time pass by reference has been removed in PHP 5.4, and you can pass uninitialised variables into functions expecting arguments by references.
 [2012-08-10 06:33 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2012-08-11 12:35 UTC] kevdevsandom at yahoo dot co dot uk
Hi Aharvey,

Thanks for your response. I believe you are quite right about the call time pass by reference being removed. A better way to solve it would be:

$rows=null;
$cols=null;
newt_get_screen_size ($rows, $cols);

"and you can pass uninitialised variables into functions expecting arguments by references"

This is not true. Maybe it's enabled in the default configuration provided with the source download, but I haven't seen any packaged version of PHP configured this way since the early 2000s. It's poor programming practise and no competent sysadmin would allow it to be put on to a production server since it leads to buggy code.

Just to re-iterate this point:
PHP Notice:  Undefined variable: rows in /home/ksandom/files/develop/mass-notready/experimentation/newt/newtExample.php on line 13
PHP Notice:  Undefined variable: cols in /home/ksandom/files/devel┌──────┐notready/experimentation/newt/newtExample.php on line 13
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 07:01:28 2024 UTC