i am trying to make a array of a object in php - php

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;
}

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).

passing an instance of a class to another class in order to use its variables

I'm trying to pass an instance of my Generator class to another class to use some of the variables. The Generator class instance works fine but when i passed it to another (SelectStrategy) class it seems it is not passing the variable at all. I'm not sure what I'm doing wrong - I used var_dump on the called function to check what it gives me but it's just blank.
Function
class Generator
{
//properties for self
private $s_charge;
public $connection;
public $task_priority;
public $fog_mode = true;
public $nodes = array();
public $type;
public function generateNodesSpecs() {
$node = array();
for ($i = 0; $i < 100; $i++) {
$charge1 = mt_rand(30,100);
$node['charge'] = $charge1;
//array_push($node, $charge1);
$hops = mt_rand(0,4);
$node['hops'] = $hops;
//array_push($node, $hops);
$resource1 = mt_rand(0,100);
if ($resource1 <= 50) {
if ($resource1 <=10){
$node['connection'] = '4G';
//array_push($node, '4G');
}
else {
$node['connection'] = '3G';
//array_push($node, '3G');
}
}
else if ($resource1 > 50 && $resource1 <= 60) {
$node['connection'] = 'WiFi';
//array_push($node, 'WiFi');
}
else {
}
$resource2 = mt_rand(0,100);
if ($resource2 <=60) {
$node['additional'] = 'CPU';
//array_push($node, 'CPU');
}
else {
$node['additional'] = 'none';
}
$this->nodes[] = $node;
//array_push($nodes, $node);
unset($node);
}
//compare which get the resources
//var_dump($this->nodes[0]);
}
class SelectStrategy {
//take in generator class instance
private $generator;
private $priority;
private $size;
private $slaves = array();
private $found_slave = null; //will hold item with max val;
public function __construct($generator) {
$this->generator = $generator;
}
private function selectSlaves() {
$max = -9999999; //will hold max val
foreach($this->generator->nodes as $k=>$v)
{
if($v['charge']>$max)
{
$max = $v['charge'];
$this->found_slave = $v;
}
}
var_dump($this->found_slave);
}
}
And classes/function calls
$generator = new Generator();
$generator->generateNodesSpecs();
$select_strategy = new SelectStrategy($generator);
$select_strategy->selectSlaves();
The $this->generator->nodes is a 2D array
global $generator;
in every function of SelectSlave should do it

PHP method to get values dynamically from an array object property

In this class, is it possible to get dynamically a value from the array?
class MyClass {
private $array_data;
function __construct() {
$this->array_data['first']['a'] = '1';
$this->array_data['second']['b'] = '2';
$this->array_data['third']['c'] = '3';
}
public function getIndexValue($index){
return $this->{'array_data' . $index};
}
}
$MyClass = new MyClass();
// Prints NULL, but i expect '1'
var_dump($MyClass->getIndexValue("['first']['a']"));
Here's a simple solution. Rather than passing in a string for the indexes, you pass in an array.
public function getIndexValue(array $indexes) {
// count the # of indexes we have
$count = count($indexes);
// local reference to data
$data = $this->array_data;
for ($i = 0; $i < $count; $i++)
{
// enter the array at the current index
$data = $data[$indexes[$i]];
}
return $data;
}
And then rather than a string, you'd pass in an array:
$MyClass->getIndexValue(['first', 'a'])

How to create objects in PHP [duplicate]

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);

PHP many objects with one Class

I'm very new with PHP and OOP techniques.
So I have no problem creating a object like so:
$member1 = new Member('person 1');
But is there a way to return a bunch of objects. So that I can iterate through them and put them in the DOM?
class Example
{
public function getPeople()
{
$objs = array();
for($i = 0; $i < 10; $i++)
{
$obj[] = new Person('Person ' . ($i + 1));
}
return $objs; // returning an array of Person objects
}
}
$example = new Example();
foreach($example->getPeople() as $person)
{
// Do something with $person
}
In order to get objects, one possible way is to use array :-
$members = new members( array(...));
// you can assess any member via $members->members[$some_id]
// class to return list of members
class members ()
{
public $members = array();
public function __construct( array $ids)
{
foreach ($id as $id)
{
$this->members[$id] = new Member($id);
}
}
}
class member()
{
public function __construct($id)
{
// any other thing you want to do
}
}

Categories