PHP Notice: undefined variable, but they are already defined - php

In my PHP script I get the error variable $command_in_hostfile undefined.
There are two functions in my class.
In the first function I return an array ($command_in_hostfile) to use it in another function.
However, I get the error "variable undefined" and I donĀ“t understand why. Can anyone help me rectify this error?
Code:
include_once('service_function.php');
if(isset($_POST['edit_cfg_file']))
{
$Host = new Host;
$Host->write_hostfile();
}
public $command_in_hostfile = array();
class Host
{
public $command_in_hostfile = array();
function read_services()
{
...
$command_in_hostfile[0] = array_merge($command_in_hostfile[0]);
$command_in_hostfile[1] = array_merge($command_in_hostfile[1]);
return($command_in_hostfile);
}
function write_hostfile()
{
foreach($command_in_hostfile[0] as $key=>$value)
{
$checkcommand[$key] = "check_command ".$value."!".$value2_converted."\n";
}
}
}
?>

try using $this->
$command_in_hostfile = array();
class Host
{
public $command_in_hostfile = array();
function read_services()
{
...
$this->command_in_hostfile[0] = array_merge($command_in_hostfile[0]);
$this->command_in_hostfile[1] = array_merge($command_in_hostfile[1]);
return($this->command_in_hostfile);
}
function write_hostfile()
{
foreach($this->command_in_hostfile as $key=>$value)
{
$checkcommand[$key] = "check_command ".$value."!".$value2_converted."\n";
}
}
}

Related

Linked list in php giving errors

Im new to PHP and I'm trying to make a linked list but it keeps on giving errors
<?php
class Node {
private $value;
private $nxt;
function __construct($x) {
$this->value = $x;
$this->set_nxt(null);
}
function set_value($x) {
$this->value = $x;
}
function get_value() {
return $this->value;
}
function set_next($x) {
$this->nxt = $x;
}
function get_next() {
return $this->nxt;
}
}
class linked_list {
private $start = new Node(null);//error is here
function __construct() {
$start = new Node(null);
}
function add_name($nme) {
$start = new Node($nme);
if ($start->get_value() == null) {
$start = new Node(nme);
$start->set_next(null);
} else {
$temp = new Node($nme);
$temp->set_next($start);
$start = $temp;
}
}
function show_all() {
$temp = $start;
while ($temp != null) {
echo $temp->get_value();
echo "<br/>";
$temp = $temp->get_next();
}
}
}
?>
It would be great if you could tell me what I am doing wrong and how I should do it right. Please I just want to know what I am doing wrong with the PHP code. There is no need to tell me about linked list I just want to know what I am doing wrong with the implementation.
Here is the data I am working with :
$list = new linked_list();
$list->add_name("first");
$list->add_name("second");
$list->add_name("third");
$list->add_name("fourth");
$list->show_all();
and here is the error :
Basically its saying the $start in linked list class is a constant. i have commented on the place the error is coming from
You have at least four errors to solve:
$this->set_nxt(null); in the constructor of the Node class: set_nxt is an undefined function (maybe you meant set_next);
private $start = new Node(null); in the linked_list class: you can't declare a property and initialise it with a new instance of a class (you can do it inside the constructor);
$start = new Node(nme); in the add_name function of the linked_list class: nme is an undefined constant (maybe you meant $name);
$temp = $start; in the show_all function of the the linked_list class: $start is an undefined variable (maybe you meant $this->start).

Send back to Ajax with OOP

New to OOP, figured I'd practice a bit by sending back data from PHP via ajax. What am I doing wrong here? It works if I change the code to procedural. Here's the OOP:
if (isset($_POST['fruity'])) {
$start_fruity = new Fruity_draft();
$start_fruity->send_json();
}
class Fruity_draft {
public $banned = $_POST['banned'];
public $players = $_POST['players'];
public $random_civs = $_POST['random_civs'];
public $array_list = [];
public $send_json['banned'] = $banned;
function __construct($send_json) {
$this->send_json = $send_json;
}
function send_json() {
echo json_encode($this->send_json);
}
}
First of all, you forgot about passing a parameter to the constructor, it expects an array.
function __construct($send_json) {
In your call, you don't send anything
$start_fruity = new Fruity_draft();
This throws a warning, Warning: Missing Argument 1
and a notice, Notice: Undefined variable: send_json
Second, you should move the initialization of the class variables in the constructor.
class Fruity_draft {
public $banned;
public $players;
public $random_civs;
public $array_list;
public $send_json;
function __construct($send_json) {
$this->banned = 'banned';
$this->players = 'players';
$this->random_civs = 'random_civs';
$this->send_json = $send_json;
$this->send_json['banned'] = $this->banned;
}
...
}
That's not really OOP :). You should return something from the class, not echo.
Also, you should send data from other function to the class.. in the constructor or with a method set_post_data() or something...
Simple:
if (isset($_POST['fruity'])) {
$start_fruity = new Fruity_draft($_POST);
echo $start_fruity->get_json_response();
}
class Fruity_draft {
private $postData;
function __construct($postData) {
$this->postData = $postData;
}
function get_json_response() {
return json_encode($this->postData['banned']);
}
}

declaring a object inside a class, giving some weird warning

I am trying to declare an object of type Spell in my class Game like this:
<?php
require 'Spell.php';
class Game
{
public $Name;
public $Spell;
function Game()
{
$Name[0] = 0;
$Spell = new Spell;
}
This is returning this warning:
"Warning: Creating default object from empty value in"
and I'm not sure why.
Try the following:
class Game
{
public $Name = array();
public $Spell;
function Game()
{
$this->Name[0] = 0;
$this->Spell = new Spell();
}
}
You should use
function Game()
{
$this->Name = [ 0 ];
$this->Spell = new Spell();
}
Check this question for more details on the error.

Need simple php oop scope matrix

EDIT I've updated the question with actual code. Turns out it was not a scope issue but a stupid mistake on my part. While testing that all value were good I was really setting them to empty.
After reading the answer below I realized I have the scope figured out but had a typo in the code.
Sorry
<?php
abstract class PHPFoo_XYZ
{
protected $_postData = array();
public function processXYZ(array $postData)
{
$this->_postData = $postData;
}
protected function _checkProcessId()
{
// doing nothing
}
}
?>
<?php
require_once dirname(__FILE__) . '/../PHPFoo/XYZ.php';
class App_XYZ extends PHPFoo_XYZ
{
protected $_UserData = array();
protected $_UserId = 'notset';
protected $_UserName = '';
public $_msg = '';
public function processXYZ(array $postData)
{
$this->_postData = $postData;
$this->_getUserData();
$this->_checkProcessId();
}
protected function _checkProcessId()
{
$this->_writeLog("User Name ".$this->_UserName);
$this->_writeLog("User Id ".$this->_UserId);
// These show empty
}
public function _getUserData() {
$UserData = array();
$UserId = array();
$User_Name = array();
$msg = '';
// Get data from database
$this->_UserId = $UserId[0]['item_id'];
// Get data from database
$this->_UserName = $User_Name[0]['title'];
// Get full data
// $results = Array of values from database
foreach ($results as $key => $value) {
$UserData[$results[$key]['fielddef_id']] = $results[$key]['value'];
}
$this->_UserData = $UserData;
$this->_writeLog("USER DATA FULL");
$this->_writeLog("User Name ".$this->_UserName);
$this->_writeLog("User Id ".$this->_UserId);
$msg = '';
foreach ($this->_UserData as $k => $v) {
$msg .= "\n".$k." == ".$v;
}
$this->_writeLog("User Data\n".$msg);
// The above output is good
if($this->_UserData = '' || $this->_UserId = '' || $his->_UserName = '') {
$this->_writeLog("There was an error getting User Data.");
return false;
}else{
return true;
}
}
}
There is something wrong from beginning, you should write "public function" when you declare a function, not "public functions", and there must be the word "function" declaring a method, not just the name.
Also you are calling a method myfunc1, when it doesn't exists and you have made another mistake when you call func2 (you wrote fucn2).
So, if you fix your code, it works as you want.
Here I fixed it for you:
<?php
abstract class foo {
protected $_var1 = '';
protected $_var2 = '';
public function func1() {
#code...
}
public function func2() {
#code..
}
}
class bar extends foo {
protected $myvar1 = '';
protected $myvar2 = '';
public function myfunc() {
// do some code to fill myvar1 to be used in other functions
$this->myvar1 = 'some data';
echo "my var " . $this->myvar1;
}
public function func2() {
// do some code that uses myvar1 data
// but $this->myvarf1 is empty here why?
echo $this->myvar1;
}
public function runit() {
$this->myfunc();
$this->func2();
}
}
//requre file
$callclass = new bar;
$callclass->runit();
?>
So please be careful before asking and if you can/want use an ide like netbeans for php to avoid this mistakes.
Have a good night.

Call to a member function get_segment() error

I'm having this problem with this piece of PHP code:
class Core {
public function start()
{
require("funk/funks/libraries/uri.php");
$this->uri = new uri();
require("funk/core/loader.php");
$this->load = new loader();
if($this->uri->get_segment(1) != "" and file_exists("funk/pages/".$uri->get_segment(1).".php")){
Only a snippet of the code
The best way I can explain it is that it is a class calling upon another class (uri.php) and i am getting the error: Fatal error: Call to a member function get_segment() on a non-object in /home/eeeee/public_html/private/funkyphp/funk/core/core.php on line 11 (the if($this->uri->get_segment(1) part)
I'm having this problem a lot and it is really bugging me.
the library code is:
<?php
class uri
{
private $server_path_info = '';
private $segment = array();
private $segments = 0;
public function __construct()
{
$segment_temp = array();
$this->server_path_info = preg_replace("/\?/", "", $_SERVER["PATH_INFO"]);
$segment_temp = explode("/", $this->server_path_info);
foreach ($segment_temp as $key => $seg)
{
if (!preg_match("/([a-zA-Z0-9\.\_\-]+)/", $seg) || empty($seg)) unset($segment_temp[$key]);
}
foreach ($segment_temp as $k => $value)
{
$this->segment[] = $value;
}
unset($segment_temp);
$this->segments = count($this->segment);
}
public function segment_exists($id = 0)
{
$id = (int)$id;
if (isset($this->segment[$id])) return true;
else return false;
}
public function get_segment($id = 0)
{
$id--;
$id = (int)$id;
if ($this->segment_exists($id) === true) return $this->segment[$id];
else return false;
}
}
?>
your calls to get_segment() are inconsistent.
In one case you call $this->uri->get_segment(), which is correct according to your previous code. The second time you call $uri->get_segment, which is missing the $this-> and so is not a valid object.

Categories