<?php
function foo($one, $two){
bar($one);
}
function bar($one){
echo $one;
//How do I access $two from the parent function scope?
}
?>
If I have the code above, how can I access the variable $two from within bar(), without passing it in as a variable (for reasons unknown).
Thanks,
Mike
Make a class - you can declare $two as an instance field which will be accessible to all instance methods:
class Blah {
private $two;
public function foo($one, $two){
this->$two = $two;
bar($one);
}
public function bar($one){
echo $one;
// How do I access $two from the parent function scope?
this->$two;
}
}
A crude way is to export it into global scope, for example:
<?php
function foo($one, $two){
global $g_two;
$g_two = $two;
bar($one);
}
function bar($one){
global $g_two;
echo $g_two;
echo $one;
//How do I access $two from the parent function scope?
}
?>
I do use $global to access variables in my entire script.
Like this:
public function exist($id) {
global $db;
$query = "SELECT * FROM web_users_session where user_id='$id'";
return $this->_check = $db->num_req($query);
}
Related
how do you define a variable in a class? seems like global only works inside the function.
<?php
$a = '20';
$b = '10';
class test {
global $a; $b;
function add() {
echo $a;
}
}
$answer = new test();
$answer->add();
?php>
i tried this one (use global inside a class but gets error instead)
also, how can you define multiple variables in just 1 line of code instead of defining it each.
To define a class property (or variable), you would do like so:
class Foo {
private $myVar = 'my var'; // define a class property
public function add() {
echo $this->myVar;
}
}
How about passing the data in via the constructor?
Code: (Demo)
$a_outside = '20';
$b_outside = '10';
class test {
public $a_inside;
public $b_inside;
public function __construct($a_passed_in, $b_passed_in)
{
$this->a_inside = $a_passed_in;
$this->b_inside = $b_passed_in;
}
public function add()
{
echo $this->a_inside + $this->b_inside;
}
}
$answer = new test($a_outside, $b_outside);
$answer->add(); // output: 30
Pass the variable to the class via arguments in the constructor call.
Define the values as variables in the class within construct call.
Access the variables within the add() method.
I am new to PHP5 and classes, am struggling with being able to get global variable to work when inside a function, to better explain it please check the code bellow.
class alpha{
#first function
public function n_one(){
#variable
$varr = 1;
#inner function
function n_two(){
global $varr;
#Unable to get variable
echo $varr;
if($varr)
{
echo 'yessssss';
}
}
echo $varr // Returns variable fine
}
}
I seem to be doing something wrong violating how classes and functions work, can't figure what is it.
Move the 'inner function', and the property.
class Alpha
{
private $varr = 1;
public function n_one()
{
// to access a property ore another method, do this
$this->varr = $this->doSomething();
return $this->varr; // Returns variable fine
}
private function doSomething()
{
// manipulate $this->varr here
}
}
Also, don't ever echo from within the class, instead return the variable and echo it.
echo $alpha->n_one();
global means to access the variable in the global scope, not just the containing scope. When you refer to $varr in the inner function it's treated as $GLOBALS['varr'].
If you want it to refer to the same variable as in the outer function you need to declare the variable global there as well. Otherwise it's a local variable in that function, while the inner function accesses the global variable.
#first function
public function n_one(){
global $varr;
#variable
$varr = 1;
#inner function
function n_two(){
global $varr;
#Unable to get variable
echo $varr;
if($varr)
{
echo 'yessssss';
}
}
echo $varr // Returns variable fine
}
Alternatively you can use the use() declaration to declare a variable that should be inherited from the outer scope.
#inner function
function n_two() use($varr) {
global $varr;
#Unable to get variable
echo $varr;
if($varr)
{
echo 'yessssss';
}
}
i made the following code:
<?php
class hoi {
public $a = 1;
function test()
{
echo $this->$a; /* reference to alocal scope variable? */
}
}
$hoi = new hoi;
$hoi->test();
?>
I try to echo $a but this does not work,
how can i echo variables declared inside the class but outside the function?
The syntax is:
$this->a
Using an additional $ in there is a "variable variable" for properties.
class hoi {
public $a = 1;
function test() {
echo $this->a;/* the variable is accessed like this - no need for the $ */
}
}
$hoi = new hoi();/* required as there is no __construct() method */
$hoi->test();
I'm working on a WordPress shortcode plugin, so I need to define a function to use with add_action('wp_footer', 'fnc_name') for example. I have created the plugin as a class with public functions and static variables.
Here's an example of what I'm trying to do (use $count in the local function tryToGetIt):
class Test {
public static $count;
public function now () {
if (!$this::$count) {
$this::$count = 0;
}
$this::$count++;
$count = (string) $this::$count;
echo 'count should be '.$count;
function tryToGetIt() {
global $count;
echo 'count is '.$count;
}
tryToGetIt();
}
};
$test = new Test();
$test->now();
You can see the demo on IDEONE: http://ideone.com/JMGIFr
The output is 'count should be 1 count is ';
As you can see I've tried declaring the $count variable with global to use the variable from the outer function, but that is not working. I've also tried $self = clone $this and using global $self within the local function.
How can the local function use the variables from within the class's public function?
This is not possible with global. PHP has exactly two variable scopes: global, and local.
<?php
$foo = 'bar'; // global scope <-----------
\
function x() { |
$foo = 'baz'; // function local scope |
|
function y() { |
global $foo; // access global scope /
echo $foo;
}
y();
}
x(); // outputs 'bar'
You COULD try a closure, e.g.
function foo() {
$foo = 'bar';
$baz = function() use (&$foo) { ... }
}
There is no practical way to access a scope defined at some intermediate level of a function call chain. You only ever have the local/current scope, and the global scope.
You could do:
function tryToGetIt($count) {
echo 'count is '.$count;
}
tryToGetIt($count);
Or to select the static variable use:
Test::$count within the tryToGetIt() function.
I tried this code, which works
class Test {
public static $count;
public function now () {
if (!$this::$count) {
$this::$count = 0;
}
$this::$count++;
$count = (string) $this::$count;
echo 'count should be '.$count;
function tryToGetIt() {
echo 'count is '. Test::$count;
}
tryToGetIt();
}
};
$test = new Test();
$test->now();
But I'm not sure I understand why you are trying to do this. Why not make tryToGetIt() a private function within Test rather than nested within now()?
I know its possible to access an object property/method using a variable as its name
ex.:
$propName = 'something';
$something = $object->$propName;
Is it possible to do the same w/ constants or static properties?
I've tried:
$constName = 'MY_CONST';
MyCLass::{$constName};
and
$obj::{$constName};
But nothing seems to work and I couldn't find it anywhere.
Use: Class::$$constName, this is similar to normal variable variables.
Demo:
<?php
class MyClass {
public static $var = 'A';
}
$name = 'var';
echo MyClass::$$name; // echoes 'A'
Constants can be access with the constant function:
constant('MyClass::'.$constantName)
This works for me:
<?php
class Test {
public static $nombre = "pepe";
public function __construct() {
return self;
}
}
$varName = "nombre";
echo Test::${$varName};
You can use the constant function:
constant('bar::'. $const);
constant("$obj::". $const); //note the double quote