Global variable inside class and function PHP - php

I'm having trouble with reading variable defined in one function in another function.
I have:
global $a;
class test{
function aa($somevar){
switch ($myvar){
case 'value':
global $a;
$a = 15;
break;
}
}
function bb(){
global $a;
echo $a;
}
}
$foo = new test();
$ccc = $foo->bb();
var_dump($ccc);
I get dump result NULL.
Thanx

At no point in your code do you assign a value to $a.
The only assignment to $a is in the test->aa method, which uses inconsistent variables and therefore even if called will never assign to $a.

You never run test->aa() to assign a value to a.
$foo = new test();
$foo->aa();
$ccc = $foo->bb();
In this case $ccc will still be null because you are echoing $a in $foo->bb() instead of returning it.
function bb() {
global $a;
return $a;
}
I would also stay away from globals and pass the variable $a on construct of the class. For example:
class test {
public $a;
function __construct($a = null) {
// pass initial var to $a if you want
$this->a = $a;
}
function aa($somevar) {
// reassign $a
$this->a = $somevar;
}
}
$foo = new test();
$foo->aa(5);
// or just $foo = new test(5);
var_dump($foo->a);

The variable $a should be a property inside the class

Here is code try this..
<?php
global $a;
class test{
function aa($somevar){
switch ($somevar){
case 'value':
global $a;
$a = 15;
break;
}
}
function bb(){
global $a;
echo $a;
return $a;
}
}
$foo = new test();
$foo->aa('value');
$ccc = $foo->bb();
var_dump($ccc);
?>

Try this :
class test
{
public $a;
function aa($somevar)
{
switch ($myvar)
{
case 'value':
$this->a = 15;
break;
}
}
function bb()
{
return $this->a;
}
}
$foo = new test();
$ccc = $foo->bb();
var_dump($ccc);
UPDATED:
<?php
class test
{
var $a;
function aa($somevar)
{
switch ($somevar)
{
case 'value':
$this->a = 15;
break;
}
}
function bb()
{
return $this->a;
}
}
$foo = new test();
$foo->aa('value');
$ccc = $foo->bb();
var_dump($ccc);
?>

Related

Returning references with or without a function

Are there any actual difference between the two ways to get the value by reference?
Way 1
<?php
class foo {
public $value = 42;
public function &getValue() {
return $this->value;
}
}
$obj = new foo;
$myValue = &$obj->getValue();
// $myValue is a reference to $obj->value, which is 42.
$obj->value = 2;
echo $myValue;
// prints the new value of $obj->value, i.e. 2.
?>
Way 2
<?php
class foo {
public $value = 42;
public function getValue() {
return $this->value;
}
}
$obj = new foo;
$myValue = &$obj->value;
$obj->value = 2;
echo $myValue;
?>
In both cases 2 is printed. So why does one need the getValue() function then? The first example is taken from the PHP Manual.
You need the first approach if class fields don't have a modifier 'public'. In this case you can't get a reference to the field outside the class. See example:
<?php
class foo
{
protected $value = 1;
public function setValue($value)
{
$this->value = $value;
}
public function &getValue()
{
return $this->value;
}
}
$obj = new foo;
$myValue = &$obj->getValue();
$obj->setValue(2);
echo $myValue;
?>

Access a variable inside a class from outside the class

I have a php class and variable as follows:
class Test(){
function fun(){
$a= 0;
$b = 5;
$sum = $b+c;
return sum;
}
}
I can access the $sum from outside as follows:
$obj = new Test();
echo $obj->fun();
How can I access the value of $b from outside class?
For this scenario you can use Object properties,
Define a public variable inside your class,
class Test(){
public $b;
function fun(){
$a= 0;
$this->b = 5;
$sum = $this->b+c;
return $sum;
}
}
$obj = new Test();
$b = $obj->b; // here null
echo $obj->fun();
$b = $obj->b; // here 5
You need to make $b as a public datamember to be able to access it from outside
class Test() {
public $b = 5;
public function fun(){
$a= 0;
$sum = $this->b + c;
return sum;
}
}
Now you have access to $b by doing this
$obj = new Test();
echo $obj->b;

PHP Fatal error: Using $this when not in object context in C:/xampp/htdocs/oops/1.php on line 9?

I need to assign b value in a inside the method onec, but its failing. Please let me know what I am doing wrong here:
<?php
class One {
public $a = 10;
public $b = 20;
public static function onec() {
$this->a = $this->b;
return $this->a;
}
}
echo One::onec();
?>
Use the self keyword. The $this keyword is not accessible under static context. Also, you should make your variables static
Like this..
<?php
class One {
public static $a = 10;
public static $b = 20;
public static function onec() {
self::$a = self::$b;
return self::$a;
}
}
echo One::onec();
You use $this in static function.
http://www.php.net/manual/en/language.oop5.static.php
<?php
class One {
public $a = 10;
public $b = 20;
public static function onec() {
$obj = new One();
$obj->a = $obj->b;
return $obj->a;
}
}
echo One::onec();
Use this code
class One {
public $a = 10;
public $b = 20;
public function onec() {
$this->a = $this->b;
return $this->a;
}
}
$obj = new One();
echo $obj->onec();

PHP Namespaces global keyword in function

<?php
namespace Top
{
$a = "Robert";
$b = "Richard";
$c = "Maurice";
function get_a()
{
global $a;
return $a;
}
function get_b()
{
global $b;
return $b;
}
function get_c()
{
global $c;
return $c;
}
echo namespace\Middle\get_a();
echo namespace\Middle\Bottom\get_c();
echo namespace\get_b();
}
namespace Top\Middle
{
$a = "Dauraun";
$b = "Khalid ";
$c = "Humberto";
function get_a()
{
global $a;
return $a;
}
function get_b()
{
global $b;
return $b;
}
function get_c()
{
global $c;
return $c;
}
}
namespace Top\Middle\Bottom
{
$a = "Terry";
$b = "Jesse";
$c = "Chris";
function get_a()
{
global $a;
return $a;
}
function get_b()
{
global $b;
return $b;
}
function get_c()
{
global $c;
return $c;
}
}
?>
So in the above code snippet I am trying to display the correct variable content using a function using the global keyword with the corresponding namespace yet, the desired result is not happening. The returned variable content is that of the namespace where the echo statement is used and not from the specified namespace. The output being 'RobertMauriceRichard.' Can someone please explain? Perhaps it's a misunderstanding on my part of the 'global' keyword inside a function that is in a namespace?
Because only 4 types of code are affected by namespace: classes, interfaces, functions, constants.
So your $a, $b, $c and echo statement are available - and actually the same - across the whole file.
By the time you call namespace\Middle\get_a();, $a is still "Robert", so "Robert" is returned.
Try to put the echo group into different namespace, and you'll observe different result:
namespace Top\Middle
{
/*...*/
echo \Top\Middle\get_a();
echo \Top\Middle\Bottom\get_c();
echo \Top\get_b();
}
/* outputs "DauraunHumbertoKhalid" */
namespace Top\Middle\Bottom
{
/*...*/
echo \Top\Middle\get_a();
echo \Top\Middle\Bottom\get_c();
echo \Top\get_b();
}
/* outputs "TerryChrisJesse" */

variable variables + objects in fields

I am trying to do something with variable variables and I got stuck on an object problem. Imagine this class setup:
class A
{
public $field = 10;
}
class B
{
public $a;
public function __construct()
{
$this->a = new A();
}
}
Now everyone knows that this pice of code works:
$a = new A();
$var = 'field';
echo $a->$var; // this will echo 10
Is there any possibility I could make something like this work?:
$b = new B();
$var = 'a->field';
echo $b->$var; // this fails
Note: any option which does not use eval function?
How about using a closure?
$getAField = function($b) {
return $b->a->field;
};
$b = new B();
echo $getAField($b);
Though, it's only possible in newer versions of PHP.
Or, as a more generic version, something like this:
function getInnerField($b, $path) { // $path is an array representing chain of member names
foreach($path as $v)
$b = $b->$v;
return $b;
}
$b = new B();
echo getInnerField($b, array("a", "field"));
You can write a custom __get method on your class to access the childrens property. This works:
class A
{
public $field = 10;
}
class B
{
public $a;
public function __construct()
{
$this->a = new A();
}
public function __get($property) {
$scope = $this;
foreach (explode('->', $property) as $child) {
if (isset($scope->$child)) {
$scope = $scope->$child;
} else {
throw new Exception('Property ' . $property . ' is not a property of this object');
}
}
return $scope;
}
}
$b = new B();
$var = 'a->field';
echo $b->$var;
Hope that helps
I don't recommend it, but you could use eval:
$b = new B();
$var = 'a->field';
eval( 'echo $b->$'.$var );
This should also work I guess:
$b = new B();
$var1 = 'a';
$var2 = 'field'
echo ($b->$var1)->$var2;

Categories