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

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.

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

Reference or call a function through variable in a php class

why can't i assign a function to a variable within a class: e.g
class call {
public $number = function() {
return 3 * 2;
}
}
$num = new call();
$num->number // expecting output 6
Is it possible to assign a method (function) to a property (variable) so that the method can be called outside the class just as a property. e.g
class call {
public $number = $this->value();
private function value() {
return 3 * 2;
}
}
$num = new call();
echo $num->$number // expecting output 6;
Use __get() magic method that called when you trying to get value of inaccessible properties
class call {
public function __get($name) {
if ($name == 'number')
return $this->value();
}
private function value() {
return 3 * 2;
}
}
$num = new call();
echo $num->number;
// 6

Extended classes and undefined Variables - PHP [duplicate]

This question already has answers here:
PHP - Private class variables giving error: undefined variable
(4 answers)
Closed 4 years ago.
Square and Rectangle are classes that extends abstract class ShapesClass, defining their own Area methods.
abstract class ShapesClass
{
// Force Extending class to define this method
abstract public function Area();
}
class Square extends ShapesClass
{
private $side = 0;
function __construct($n)
{
$side = $n;
}
function Area()
{
echo $side * $side;
}
}
class Rectangle extends ShapesClass
{
var $length = 0;
var $width = 0;
function __construct($a,$b)
{
$length = $a;
$width = $b;
}
function Area()
{
echo $length * $width;
}
}
$listShapes = array();
$listShapes[0] = new Square(3);
$listShapes[1] = new Rectangle(3,4);
$listShapes[0]->Area();
$listShapes[1]->Area();
I get undefined variable errors on side, length and width.
Doesn't these three variables have default values and are also set by the constructor.
You actually want to refer to them as $this->foo
function __construct($a,$b)
{
$this->length = $a;
$this->width = $b;
}
Basically $foo acts like a local, variable, scoped to current method. $this->foo acts like a instance variable, which is available anywhere in the class.
In your code whenever we create variables for any class we can call them inside methods with the class reference only i.e. $this
Also, in php we can define the variables with var there are public, protected and private keywords to define any variable.
So, you can update your code with the below code
abstract class ShapesClass
{
// Force Extending class to define this method
abstract public function Area();
}
class Square extends ShapesClass
{
private $side = 0;
function __construct($n)
{
$this->side = $n;
}
function Area()
{
echo $this->side * $this->side;
}
}
class Rectangle extends ShapesClass {
private $length = 0;
private $width = 0;
function __construct($a,$b)
{
$this->length = $a;
$this->width = $b;
}
function Area()
{
echo $this->length * $this->width;
}
}
$listShapes = array();
$listShapes[0] = new Square(3);
$listShapes[1] = new Rectangle(3,4);
$listShapes[0]->Area();
$listShapes[1]->Area();

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

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

Categories