How to create objects in PHP [duplicate] - php

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

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

Why aren't my variables printing? PHP [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 5 years ago.
Could anyone tell me why my character stats will not print? I'm not getting any error or syntax messages..
This is my first php project ever. I can't figure out what I'm missing!
<?
class character{
public $healthpoints = 100;
public $isdead = false;
public $class = "Mage";
public $level = 10;
}
function checkdeath() {
if($healthpoints >= 0){
$isdead = true;
}
}
new character();
function CharStats() {
echo $healthpoints;
echo $isdead;
echo $class;
echo $level;
}
CharStats;
?>
I can't figure out what I'm missing!
Thinking, ALL
class Character{
public $healthpoints = 100;
public $isdead = false;
public $class = "Mage";
public $level = 10;
function checkdeath() {
if($this->healthpoints >= 0){
$this->isdead = true;
}
}
function CharStats() {
echo $this->healthpoints;
echo $this->$isdead;
echo $this->$class;
echo $this->$level;
}
}
$character = new Character();
$character->ChatStats();
Read again about classes/objects/etc. - Classes and Objects in PHP

How to check if a function has a value [duplicate]

This question already has answers here:
How do you create optional arguments in php?
(7 answers)
Closed 8 years ago.
I am not sure if the title is correct, my problem is that I have a class and functions inside it, I would like to check if the value for the function is set and if not set other value
class some_class
{
private $width;
function width( $value )
{
// Set another default value if this is not set
$this->width = $value;
}
}
$v = new some_class();
// Set the value here but if I choose to leave this out I want a default value
$v->width( 150 );
This might be what you're looking for
class some_class
{
function width($width = 100)
{
echo $width;
}
}
$sc = new some_class();
$sc->width();
// Outputs 100
$sc->width(150);
// Outputs 150
You can do something like this:
class SomeClass
{
private $width;
function setWidth($value = 100)
{
$this->width = $value;
}
}
$object = new SomeClass();
$object->setWidth();
echo '<pre>';
print_r($object);
Will result into like this if empty:
SomeClass Object
(
[width:SomeClass:private] => 100
)
or something like this too:
class SomeClass
{
private $width;
function setWidth()
{
$this->width = (func_num_args() > 0) ? func_get_arg(0) : 100;
}
}
$object = new SomeClass();
$object->setWidth();
echo '<pre>';
print_r($object); // same output
Try this
class some_class
{
private $width;
function width( $value=500 ) //Give default value here
{
$this->width = $value;
}
}
Check Manual for default value.

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'])

i am trying to make a array of a object in 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;
}

Categories