This question already has answers here:
Access variable from scope of another function?
(3 answers)
Closed 5 years ago.
I have this code
class foo
{
function one()
{
$number = 1;
}
function two()
{
echo $number;
}
}
And i want to call $number from function one() on function two().
Is it possible to do this ?
Hello Mfdsix Indo,
Try this code,
class foo
{
function one()
{
$number = 1;
return $number;
}
function two()
{
echo $this->one();
}
}
OR
class foo
{
private $number;
function one()
{
$this->number = 1;
}
function two()
{
echo $this->number;
}
}
I hope my answer id helpful.
If any query so comment please.
Related
This question already has answers here:
Are objects in PHP assigned by value or reference?
(3 answers)
Are PHP5 objects passed by reference? [duplicate]
(8 answers)
Closed 2 years ago.
what i want to ask is, why the $character property of Forest class is a reference instance to $character Object not the clone ?
<?php
// example code
Class Ogre
{
protected $position = 0;
protected $name;
public function setPosition($position)
{
$this->position = $position;
}
public function getPosition()
{
return $this->name.' position is at axis '.$this->position.'.';
}
public function setName($name)
{
$this->name = $name;
}
public function walk($step)
{
$this->position += $step;
return $this;
}
public function getInfo()
{
return 'Type: '.self::class.', Name: '.$this->name;
}
}
class Forest
{
protected $character;
public function __construct($character)
{
// var_dump($character);
$character->setPosition(55);
}
public function getName()
{
return 'Theme Name : Forest';
}
}
$character = new Ogre();
$character->setName('Magi');
echo $character->getPosition(); //the position is 0 i've never defined the position
$theme = new Forest($character);
echo $character->getPosition(); //it shows 55 because i defined it inside Forest constructor
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
This question already has answers here:
Mysql - Parse error (syntax error) in my code [duplicate]
(2 answers)
Closed 7 years ago.
Is this possible?
class Foo {
public function bar() {
return true;
}
}
class Foo2 {
$fooey = new Foo;
public function bar2() {
if ( $fooey->bar ) {
return 'bar is true';
}
}
}
I realize the above would not work because I need to get $fooey inside the scope of bar2. How would I do this?
Thanks in advance.
You can't create an object in a class outside of the functions, so use __construct as that will run first when the object is created.
<?php
class Foo {
public function bar() {
return true;
}
}
class Foo2 {
private $fooey = null
public __construct() {
$this->fooey = new Foo();
}
public function bar2() {
if ( $this->fooey->bar ) {
return 'bar is true';
}
}
}
?>
What you have is not valid PHP syntax. I believe you're looking for something like this:
class Foo {
public function bar() {
return true;
}
}
class Foo2 {
private $fooey;
public function __construct() {
$this->fooey = new Foo;
}
public function bar2() {
if ( $this->fooey->bar() ) {
return 'bar is true';
}
}
}
$obj = new Foo2;
$obj->bar2(); // 'bar is true' will be printed
You need to initialize things in the constructor (or pass it in as a variable).
You need to use $this to refer to own properties.
This question already has an answer here:
Overloading method of comparison for custom class
(1 answer)
Closed 9 years ago.
I was wondering if there is a way to create a class in PHP that when compared with other variables a default value is used instead of the class itself? such that:
class Test {
private $name;
private $val;
public function __construct($name, $val) {
$this->name = $name;
$this->val = $val;
}
public __default() {
return $val;
}
public function getName() {
return $name;
}
}
then I could use a function like __default when I compare it to another value such as:
$t = new Test("Joe", 12345);
if($t == 12345) { echo "I want this to work"; }
the phrase "I want this to work" will print.
As far as I know this is not possible. The closest thing you're looking for is the __toString() method to be set on the class.
http://php.net/manual/en/language.oop5.magic.php
PHP might try to convert it to an Integer, but I'm not sure if there are class methods to accomplish this. You could try string comparison.
<?php
class Test {
private $name;
private $val;
public function __construct($name, $val) {
$this->name = $name;
$this->val = $val;
}
public function __toString() {
return (string)$this->val;
}
public function __toInt() {
return $this->val;
}
public function getName() {
return $this->name;
}
}
$t = new Test("Joe", 12345);
if($t == '12345') { echo "I want this to work"; }
The __toString magic method will do what you want with some caveats:
class Test {
private $name;
private $val;
public function __construct($name, $val) {
$this->name = $name;
$this->val = $val;
}
public function __toString() {
return $this->val;
}
public function getName() {
return $this->name;
}
}
Objects can't be directly cast to an integer so will always get a when comparing to an integer but if you cast either side of the comparison to a string it will work as expected.
if($t == 12345) // false with a warning about can't cast object to integer
if((string)$t == 12345) // true
if($t == "12345") // true
Implement __toString() in your class.
Like:
class myClass {
// your stuff
public function __toString() {
return "something, or a member property....";
}
}
Your object will unlikely equals integer. But you can implement something similar to Java's hashCode() - a class method that do some math to produce numeric hash - a return value based on i.e. its internal state, variables etc. Then compare these hash codes.
Why not something along this line:
class Test {
private $name;
private $val;
public function __construct($name, $val) {
$this->name = $name;
$this->val = $val;
}
public __default() {
return $val;
}
public compare($input) {
if($this->val == $input)
return TRUE;
return FALSE;
}
public function getName() {
return $name;
}
}
$t = new Test("Joe", 12345);
if($t->compare(12345)) { echo "I want this to work"; }
From other answers it appears there is not a built in function to handle this.
function echo_parent_func() {
echo // so what now ?
}
function somefunc() {
echo_parent_func();
}
somefunc(); //should echo string 'somefunc'
Is that even possible with php ?
function get_caller_method()
{
$traces = debug_backtrace();
if (isset($traces[2]))
{
return $traces[2]['function'];
}
return null;
}
function echo_parent_func() {
echo get_caller_method();
}
function somefunc() {
echo_parent_func();
}
somefunc(); //should echo string 'somefunc'
Source
EDIT Just found this answer too: