how to access variables in a class but outside a function? - php

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

Related

outer variables define inside a class

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.

Create Instance variable in Laravel

I have a php class which having 4 methods.All the 4 methods are using some common variables, I've created those variables as instance variable. But i'm getting an error like "Undefined variable: " How can i solve this problem.
My code is,
public class test{
public static $variable;
public function func(){
$variable = "Hello World";
print_r($variable);
}
}
Actually this code will not gives you any error as you just print the variable which you have defined exactly above print statement.
Demo : http://sandbox.onlinephpfunctions.com/code/cd43e866591ee0693cdcbeec6a230f583f756a67
If you want to assign value to $variable which is defined above the function then try this code :
<?php
class test {
public static $variable;
public function func(){
self::$variable = "Hello World";
print_r(self::$variable);
}
}
$n = new test();
echo $n->func();
?>
Demo : http://sandbox.onlinephpfunctions.com/code/f11b726a678b9a5ee0e474d7ca194bb5ed75af22
If you have same static value for that variable try this
class test
{
const variable ="Hello World";
public function func()
{
echo self::variable;
}
}
$name = new test;
$name->func();

How to use class function variable within a local function

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()?

Access static object property through variable name

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

Access variable from scope of another function?

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

Categories