PHP use class variable in a class array - php

I am trying to use a variable in an array within my class
class MyClass {
public static $a = "World";
public static $b = array("Hello" => self::$a);
}
This code doesn't work. Should I be using MyClass::$a or something else. Any ideas?

You probably can instatiate them at runtime:
class MyClass {
public static $a;
public static $b;
}
MyClass::$a = "World";
MyClass::$b = [ "Hello" => MyClass::$a ];
Or you can create a static initialization method:
class MyClass {
public static $a;
public static $b;
public static function init(){
static::$a = "World";
static::$b = [ "Hello" => static::$a ];
}
}
MyClass::init();

If your $a won't change, make it a constant
class MyClass {
const a = "World";
public static $b = array("Hello" => self::a);
}
var_dump(MyClass::$b);

class MyClass {
public static $a = "World";
public static $b;
public function __construct(){
self::$b = array("Hello" => self::$a);
}
}
$obj = new MyClass();

Related

Accessing constant of an injected class in PHP

class A{
const MY_CONSTANT = 'my constant';
}
class B{
protected $a;
public function __construct(A $a)
{
$this->a = $a;
}
public function someFunction()
{
return $this->a::MY_CONSTANT;
}
}
Why the constant is not accessible like this way - $this->a::MY_CONSTANT? Anybody knows any other ways?
The above can be achieved in this way. Here we are using get_class function to get classname as string. which we are storing it in a variable and then retrieve the value of constant by using that variable.
Try this code snippet here
<?php
ini_set('display_errors', 1);
class A{
const MY_CONSTANT = 'my constant';
}
class B{
protected $a;
public function __construct(A $a)
{
$this->a = $a;
}
public function someFunction()
{
$class=get_class($this->a);
echo $class::MY_CONSTANT;
}
}
$object=new B(new A());
$object->someFunction();
You can also do the same by this approach.
class A{
const MY_CONSTANT = 'my constant';
public function __get($key){
$r = new ReflectionObject($this);
if($r->hasConstant($key)){ return $r->getConstant($key); }
}
}
class B{
public function someFunction()
{
return new A();
}
}
$b = new B();
var_dump($b->someFunction()->MY_CONSTANT);
Original Answer Link

Get property from another class function?

<?php
//file classA.php
class A {
private $B;
public $data;
public function __construct(){
$this->B = new B();
}
public function readA(){
$this->data = $this->B->readB();
print $this->data;
}
public function sendB(){
return "WORD";
}
}
//file classB.php
class B {
private $A;
public function __construct(){
$this->A = new A();
}
public function readB(){
return $this->A->sendB();
}
}
require_once .... classA.php
requier_once .... classB.php
$classA = new A();
$classA->readA();
I wanna use classes with multiple dependences.
Can't use instance methods or extends classes.
How can i get function result and send it back to the same class from another?
Your problem is an endless loop of object creation:
When you create class A it'll create an Object of Class B, that will create another object of Class A again, that will create an Object of Class B,....
-> Memory Error
So if you get rid of $this->B = new B(); in __construct() of class A it'll work with that change:
You had:
// in class B
public function readB(){
return $this->sendB();
}
// it needs to be:
public function readB(){
return $this->A->sendB();
}
Complete working code:
EDIT: Now with readA() in class A, but creation of class B out of constructor.
<?php
class A {
private $B;
public $data;
public function __construct(){
// $this->B = new B();
}
public function readA(){
$this->B = new B();
$this->data = $this->B->readB();
print $this->data;
}
public function sendB(){
return "WORD";
}
}
//file classB.php
class B {
private $A;
public function __construct(){
$this->A = new A();
}
public function readB(){
return $this->A->sendB();
}
}
$B = new B();
echo $B->readB();
$A = new A();
echo $A->readA();
?>

PHP Class with properties that has objects be accessible to these objects

I have a main php class such as this:
class MyClass {
public $a;
public $b;
function __construct()
{
$this->a = new \SomeClass();
$this->b = 'some string';
}
}
is there a way the class which is stored in the $a (SomeClass) property can access the $b value which is actually a property which is stored in the class that initiated $a (MyClass) ?
You could do something like this:
class MyClass {
public $a;
public $b;
function __construct()
{
$this->a = new \SomeClass($this);
$this->b = 'some string';
}
}
class SomeClass {
public $mc;
function __construct(MyClass $mc)
{
$this->mc = $mc;
}
}
$myClass = new MyClass();
echo $myClass->a->mc->b;
The output would be: some string
You can do something like this:
class MyClass {
public $a;
public $b;
function __construct()
{
$this->b = 'some string';
$this->a = new \SomeClass($this->b);
}
}
class SomeClass{
function __construct($b)
{
echo $b; // it will become a $this->b as given while creating new class in MyClass
}
}

PHP How to access variable from other variable inside class?

Here is my class:
<?php
class myClass {
private $a = 1;
private $b = array(
'a' => $this->a
);
public function getB() {
return $this->b;
}
}
$myclass = new myClass();
var_dump($myclass->getB());
I want to access variable $a in variable $b. But this shows this error:
( ! ) Parse error: syntax error, unexpected '$this' (T_VARIABLE) in
C:\xampp\htdocs\test1.php on line 5
You are not allowed to assign a variable property this way. The best way is to probably assign the variable to the array in the constructor instead. So, like this:
<?php
class myClass {
private $a = 1;
private $b = array();
public function __construct() {
$this->b['a'] = $this->a;
}
public function getB() {
return $this->b;
}
}
$myclass = new myClass();
var_dump($myclass->getB());
You can access variables by constructor.
Here is some code:
class myClass {
private $a;
private $b;
public function __construct(){
$this->a = 1;
$this->b = array('a'=>$this->a);
}
public function getB() {
return $this->b;
}
}
$myclass = new myClass();
var_dump($myclass->getB());

Access static variable in non-static method + Inheritance

I have the following stucture
class Foo
{
public static $a = "parent";
public static function Load()
{
return static::$a;
}
public function Update()
{
return self::$a;
}
}
class Bar extends Foo
{
private static $a = "child";
}
I want the Update function to be able to return $a aswell, but I can't get it to work.
Bar::Load(); //returns child, Correct.
$bar = new Bar();
$bar->Update(); //returns parent, Wrong.
I've tried self:: , static:: and get_class() without success.
Change self::$a in update()
class Foo
{
protected static $a = "parent"; // Notice this is now "protected"
public function child()
{
return static::$a;
}
public function parent()
{
return self::$a;
}
}
class Bar extends Foo
{
protected static $a = "child"; // Notice this is now "protected"
}
$bar = new Bar();
print $bar->child() . "\n";
print $bar->parent() . "\n";
See my code
class Foo
{
protected static $a = "parent";
public static function Load()
{
return static::$a;
}
public function Update()
{
return static::$a;
}
}
class Bar extends Foo
{
protected static $a = "child";
}
Bar::Load(); //returns child, Correct.
$bar = new Bar();
$bar->Update(); //returns child, Correct.

Categories