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).
Related
First of all, I want to write two class.
<?php
class A
{
function load($v)
{
$this->$v = "stack";
}
}
class Base
{
var $lib = null;
function __construct()
{
$this->lib = new A();
}
}
$bs = new Base();
$bs->lib->load("libx");
// Current calling method
$bs->lib->libx;
// But I want this
$bs->libx;
?>
I write a MVC fremawork.
CodeIgniter can do this but I could not.
My english is poor. Because of this, don't talk me complicated please.
You can add a magic __get method to Base to look for the property in lib, and return it if it exists:
class A
{
function load($v)
{
$this->$v = "stack";
}
}
class Base
{
var $lib = null;
function __construct()
{
$this->lib = new A();
}
public function __get($v)
{
if (property_exists($this->lib, $v)) {
return $this->lib->$v;
}
}
}
Usage:
$bs = new Base();
$bs->lib->load("libx");
echo $bs->lib->libx, PHP_EOL;
echo $bs->libx, PHP_EOL;
Output:
stack
stack
I'm trying to make an array of Spells.
My current code
class Spell
{
public $bomb = 0;
public $fire = 0;
function Spell()
{
$this->bomb =0;
$this->fire =0;
}
}
And I declare the object spell on my game class like this
class game
{
public $Spell=array();
function Game()
{
$this->Spell[0] = new Spell();
}
function s()
{
$this->Spell[1]->$bomb = $load($x)
$this->Spell[1]->$fire = $load($x);
$this->Spell[2]->$bomb = $load($y)
$this->Spell[3]->$bomb = $load($z)
}
}
It returns this error -- Warning: Creating default object from empty value in...
I guess this isn't the best way to create an array of objects. How to do it properly?
EDIT:
x y z, just return strings
The problem is that you have not created objects for $this->Spell[1], $this->Spell[2] and $this->Spell[3]. If you change your Game() constructor to this:
function Game()
{
for ($i = 1; $i <= 3; $i++) {
$this->Spell[$i] = new Spell();
}
}
It should probably work fine.
You seem to have more than just one problems in your code.
However, I will discuss the one you have asked the question for.
Instead of
$this->Spell[1]->$bomb = something;
Use
$this->Spell[1]->bomb = something;
Second, What do you intend to do by using $load($y)?
If you're using a function named "load", use load($y)
you must create object, then use it, look:
class Spell
{
public $bomb = 0;
public $fire = 0;
function __construct()
{
$this->bomb =0;
$this->fire =0;
}
}
class game
{
public $Spell=array();
function s()
{
$this->Spell[1] = new Spell();
$this->Spell[1]->bomb = 0 ; //or other value
}
}
<?php
class Spell
{
public $bomb = 0;
public $fire = 0;
function Spell()
{
$this->bomb =0;
$this->fire =0;
}
}
class game
{
public $Spell=array();
function Game($index)
{
$this->Spell[$index] = new Spell();
echo 'constructer called';
}
function s()
{
$this->Spell[1]->bomb = $load($x);
$this->Spell[1]->fire = $load($x);
$this->Spell[2]->bomb = $load($y);
$this->Spell[3]->bomb = $load($z);
}
}
$ob = new game();
//$ob->Game(1); to pass the index for array.
?>
You are using lots of undefined stuff, I would say the half of your script is missing.
I just added the comments down here:
class game
{
public $Spell=array();
function Game()
{
$this->Spell[0] = new Spell();
}
function s()
{
/**
down here you are using these undefined "variables":
$bomb
$load
$x
$y
$z
undefined means, you are using a varible which was not declared. so it´s just null.
I tried to fix it:
**/
$x = 1;
$y = 2;
$z = 3;
$this->Spell[1] = new Spell();
$this->Spell[2] = new Spell();
$this->Spell[3] = new Spell();
$this->Spell[1]->bomb = load($x); // add ;
$this->Spell[1]->fire = load($x);
$this->Spell[2]->bomb = load($y)
$this->Spell[3]->bomb = load($z)
}
}
function load($v)
{
return $v * 2;
}
This question already has answers here:
How to define an empty object in PHP
(17 answers)
Closed 10 years ago.
I'm having difficulty understanding how to create objects in my script.... i get this error :
PHP Fatal error: Call to undefined function Object()
My code is like this:
$block = Object(); // error here
$row['x'] = 5;
$row['y'] = 7;
$row['widthx'] = 3;
$row['widthy'] = 3;
for($i = $row['x']; $i < ($row['x'] + $row['widthx']); $i++){
if(!is_object($block[$i])){
$block[$i] = Object();
}
}
Can some one explain what i'm doing incorrectly?
In the simplest form, objects are classes.
class coOrds {
// create a store for coordinates
private $xy;
function __contruct() {
// it's still an array in the end
$this->xy = array();
}
function checkXY($x, $y) {
// check if xy exists
return isset($this->xy[$x][$y]);
}
function saveXY($x, $y) {
// check if XY exists
if ($this->checkXY) {
// it already exists
return false;
} else {
// save it
if (!isset($this->xy[$x])) {
// create x if it doesn't already exist
$this->xy[$x] = array();
}
// create y
$this->xy[$x][$y] = '';
// return
return true;
}
}
}
$coords = new coOrds();
$coords->saveXY(4, 5); // true
$coords->saveXY(5, 5); // true
$coords->saveXY(4, 5); // false, already exists
Start reading about them here: http://www.php.net/manual/en/language.oop5.basic.php
You need to define classes and instance them as objects:
class Object {
private $name;
__construct($name){
$this->name=$name
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
}
$block = $new Object($name);
How can i pass a class as a parameter in my function
So far i've tried
$sc = new SampleClass();
SampleFunction($sc);
function SampleFunction(&$refClass)
{
echo $refClass->getValue();
}
this is a simplified example of what im doing.. i actually have to do complex procedures inside this sample function. I'm not getting any response from the sample function. What am i doing wrong? thank you
UPDATE
char.php
class Charss {
var $name=0;
var $hp=500;
var $spd=10;
var $rtime=10;
var $dmg=10;
function __construct( $name, $hp, $spd, $rtime , $dmg) {
$this->name = $name;
$this->hp = $hp;
$this->spd = $spd;
$this->rtime = $rtime;
$this->dmg = $dmg;
}
function get_name() {
return $this->name;
}
function set_name($new_name) {
$this->name = $new_name;
}
function get_hp() {
return $this->hp;
}
function set_hp($new_hp) {
$this->hp = $new_hp;
}
function get_spd() {
return $this->spd;
}
function set_spd($new_spd) {
$this->spd = $new_spd;
}
function get_rtime() {
return $this->rtime;
}
function set_rtime($new_rtime) {
$this->rtime = $new_rtime;
}
function get_dmg() {
return $this->get_dmg;
}
function set_dmg($new_dmg) {
$this->dmg = $new_dmg;
}
}
myclass.php
require("char.php");
class Person {
function try_process()
{
$chr1 = new Charss("Player1",500,3,0,50);
$chr2 = new Charss("Player2",500,6,0,70);
while ($chr1->get_hp() > 0 && $chr2->get_hp() > 0)
{
$sth = min($chr1->get_rtime(), $chr2->get_rtime());
if ($chr1->get_rtime() == 0 && $chr2->get_rtime() > 0)
{
exit;
Fight($chr1,$chr2);
$chr1->set_rtime($chr1->get_spd());
}
elseif ($chr2->get_rtime() == 0 && $chr1->get_rtime() > 0)
{
Fight($chr2,$chr1);
$chr2->set_rtime($chr2->get_spd());
}
else
{
Fight($chr1,$chr2); #having trouble with this
$chr1->set_rtime($chr1->get_spd());
}
$chr1->set_rtime($chr1->get_rtime() - $sth);
$chr2->set_rtime($chr2->get_rtime() - $sth);
}
}
function Fight($atk,$def)
{
$def->set_hp($def->get_hp() - $atk->get_dmg());
echo $atk->get_name() . " attacked " . $def->get_name() . " for " . $atk->get_dmg() . " damage";
}
}
so im calling the function try_process on button click
What you're actually doing there is passing an object, not a class.
$sc = new SampleClass();
creates an instance of SampleClass, aka an object.
I assume there's some error being thrown elsewhere as what you have is correct.
I tested the following code and got the expected output:
class SampleClass
{
public function getValue()
{
return 4;
}
}
$sc = new SampleClass();
SampleFunction($sc);
function SampleFunction(&$refClass)
{
echo $refClass->getValue();
}
Output: 4
If you provide more details of your actual code we might be able to determine the problem.
I can't see anything wrong with your code
using &$refClass is however is not recommended and I guess willbe removed from future iteration of PHP version
but here is an example
class objects are passed as reference I suppose so no need of '&'
http://ideone.com/GbmUy
Why is the function argument a reference? Probably shouldn't be.
Other than that, there's nothing wrong with you posted, so the error is likely within SampleClass.
Others have answered pretty well, but this is a silly little example to show you how to modify the class (either by calling a property setter, or setting public properties directly)
class foo {
private $member1;
public $member2;
public function __construct($member1,$member2) {
$this->member1=$member1;
$this->member2=$member2;
}
public function SetMember1($value) {
$this->member1 = $value;
}
public function GetMember1() {
return $this->member1;
}
}
function SetMembers(foo $obj, $member1, $member2) {
// Call a setter
$obj->SetMember1($member1);
// Set a member variable directly
$obj->member2 = $member2;
}
$obj = new foo('default member 1', 'default member 2');
echo "member1 (before): {$obj->GetMember1()}\n";
echo "member2 (before): {$obj->member2}\n";
// Change values
SetMembers($obj, 'new member1', 'new member2');
echo "member1 (after): {$obj->GetMember1()}\n";
echo "member2 (after): {$obj->member2}\n";
This will output:
member1 (before): default member 1
member2 (before): default member 2
member1 (after): new member1
member2 (after): new member2
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.