Access a variable inside a class from outside the class - php

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;

Related

Is there any way I can change class variable, which would affect all instances created after?

class foo
{
public $bar = 1;
}
#foo.bar = 2; //change class variable and affect all after
$a = new foo();
$a->bar = 2;//avoid DRY
$b = new foo();
$b->bar = 2;//avoid DRY
echo $a->bar;
echo $b->bar;
Is there any way I can change class variable, which would affect all instances created after?
I want to change one default value, but I don't want to keep repeating every time I instantiate a new object.
You'd need to make the variable static.
class foo
{
public static $bar = 1;
}
$a = new foo();
$a::$bar = 2;
$b = new foo();
echo $b::$bar; // 2
You can also do Foo::$bar = 2; to set the variable for all instances.
Having a static property works great if the constructor should be called each time. If you're attempting to chain invocations, however, you might use clone instead.
class Foo
{
public $bar = 1;
public function bar(?int $bar = null): Foo
{
$this->bar = $bar ?? $this->bar;
return $this;
}
public function copy(?int $bar = null): Foo
{
return $this->fork()->bar($bar ?? $this->bar);
}
public function fork(): Foo
{
return clone $this;
}
}
$a = new foo();
$a2 = $a->copy();
$b = $a->copy(2);
$b2 = $a->copy();
$c = $b->copy(3);
$c2 = $b->copy();
https://3v4l.org/Fevli
Note, there is also a __clone() magic method that allows you to configure the actual cloned properties that go along (e.g., reset or increment, etc).
Here's method using static::with() methodology, which might fit best for the DRY approach of preconfiguration.
class Foo
{
public const DEFAULT_BAR = 5;
public $bar = FOO::DEFAULT_BAR;
public function bar(?int $bar = null): Foo
{
$this->bar = $bar ?? $this->bar;
return $this;
}
static public function withBar(?int $bar = null): Foo
{
return (new self())->bar($bar ?? Foo::DEFAULT_BAR);
}
public function fromBar(?int $bar = null): Foo
{
return (new self())->bar($bar ?? $this->bar);
}
public function fork(): Foo
{
return clone $this;
}
}
$a = Foo::withBar();
$b = Foo::withBar(4);
$b2 = $b->fromBar();
$c = Foo::withBar();
https://3v4l.org/qI8SY

Laravel: accessing variable declared outside of a function

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.

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

Global variable inside class and function 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);
?>

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