<?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" */
Related
I want to have a variable that may be used inside and outside of the functions. On PHP manual I found the following example:
http://php.net/manual/ro/language.variables.scope.php
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
Manual says:
The above script will output 3.
But my laravel output for this code (in public show function inside a contreller) is 2.
How to make this work as needed?
Try this Code
class TestController extends Controller {
private $search;
public function __construct() {
$this->search = 1;
}
public function global () {
echo $this->search;
}
I solved it by doing so:
*/
public function show(Plan $plan)
{
global $a;
global $b;
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b.'<br>';
So, the idea is to use global on every function.
For example I have a class like this
public function data() {
$a = 1;
$b = 2;
$c = 3;
}
public function codeigniter() {
$a, $b, $b //how i can get the value??
}
How can I get the value of $a, $b, $c so that I can use it in codeigniter() function? I use codeigniter? Do I need to add a return;?
I assume the two function are in both class and in a controller class of codeigniter
class Sample extends CI_Controller {
// declare variables
public $a;
public $b;
public $c;
public function __construct() {
// call codeigniter method
$this->codeigniter();
}
public function data() {
$this->a = 10;
$this->b = 11;
$this->c = 12;
}
public function codeigniter() {
// call method data();
$this->data();
echo $this->a; // prints 10
echo $this->b; // prints 11
echo $this->c; // prints 12
}
}
First create a array and store the values in it and return the array.
public function data() {
$data = array();
$data['a']=1;
$data['b']=2;
$data['c']=3;
return $data;
}
Then you can simply call the function and access the returned data.
public function codeigniter() {
$data_value = $this->data();
echo $data_value['a'];
echo $data_value['b'];
echo $data_value['c'];
}
To get more than one variable from a function you must place those variables in an array and return the array
public function data() {
$a = 1;
$b = 2;
$c = 3;
$data = array();
array_push($data, $a,$b,$c);
return $data;
}//data function ends
public function codeigniter() {
$data = $this->data();
//now use $data
}
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);
?>
This is my first program in oop php. Its very simple where i would like to add a numerical value to a variable. And program must output 2.
<?php
class MyClass
{
public $a = 1;
public function abc()
{
if ($a=1){
$a+1;
}
}
}
$obj = new MyClass;
echo $obj->abc;
?>
In addition to gview's answer:
if ($a=1){
$a+1;
}
Should be:
if ($a == 1){
$a = $a + 1;
}
The = operator is for assignment, not for comparisons.
The abc() function does not return anything. Thus you get no output. If you add:
return $a;
You'll get something in the echo.
You aren't returning your results
public function abc()
{
if ($a==1){
$a++;
}
return $a;
}
I think you forgot to return the value from abc()
public function abc()
{
if ($a=1){
$a+1;
}
return $a;
}
suppose i have two nested function like this :
$a = 1;
$b = 2;
function test(){
$b = 20;
function Sum()
{
$b = $GLOBALS['a'] + $b;
}
}
test();
Sum();
echo $b;
now i want in function Sum() access to $b variable declared in function test();
How do you do?
Wild-Guessing-mode:
Your function Sum() would "normaly" take two parameters/operands like
function Sum($a, $b) {
return $a+$b;
}
echo Sum(1, 20);
Now you have the function Test() and you want it to return a function fn that takes only one parameter and then calls Sum($a, $b) with one "pre-defined" parameter and the one passed to fn.
That's called either currying or partial application (depending on what exactly you implement) and you can do something like that with lambda functions/closures since php 5.3
<?php
function Sum($a, $b) {
return $a + $b;
}
function foo($a) {
return function($b) use ($a) {
return Sum($a, $b);
};
}
$fn = foo(1) // -> Sum(1, $b);
$fn = foo(2) // -> Sum(2, $b);
echo $fn(47);
Why not use this?
$a = 1;
$b = 2;
function test(){
$b = Sum(20);
}
function Sum($value)
{
$value = $GLOBALS['a'] + $value;
return $value;
}
test();
// Sum(); // Why do you need this here??
echo $b;
Edit: Better without globals
$a = 1;
$b = 2;
function Sum($value1, $value2)
{
return $value1 + $value2;
}
$b = 20; // you could call Sum($a, 20); instead
$b = Sum($a, $b);
echo $b;
According to the Context best way is to pass the variable to the function like this
Sum($b)
But if you are looking for an alternative then you can use closures
but REMEMBER PHP<5.3 does not support closures
You can do
$a = 1;
$b = 2;
function test() {
$b = 20;
function Sum() use($b)
{
$b = $GLOBALS['a'] + $b;
}
}
test();
Sum();
echo $b;