Accessing constant of an injected class in PHP - 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

Related

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 output function return through classes

this is my code, semplified
I'm trying to output something, returned form other functions, but it doesn't work.
I've tried also using self:: instead of $this->
class a{
function a(){
return "aaaaaaaaaaaaaaaaah";
}
function b(){
return "bbbbbbb";
}
}
$a = new a;
class b{
function ea($a){
return "oajs$a";
}
function f(){
global $a;
$blah = $this->ea("asd");
$blah .= $a->a;
return $blah;
}
}
$b = new b;
echo $b->f;
This is what you are looking for.
class a {
public function a(){
return 1;
}
public function b(){
return 2;
}
}
class b{
public $a;
public function __construct() {
$this->a = new a();
}
public function ea($a){
return "ao$a";
}
public function f(){
$blah = $this->ea("stuff");
$blah .= $this->a->b();
return $blah;
}
}
$b = new b();
echo $b->f();

PHP Use child variable to create a class in parent class

I have 3 classes:
Class A - Parent Class
Class B - Child Class
Class C - Class to be used in Class A
I want to use functions from class C using variables from my Child class.
<?php
class A
{
public function __construct()
{
$this->load();
}
public function load()
{
$class = new C();
$class->test = $this->test;
$this->c = $class;
}
}
class B extends A
{
public function __construct()
{
parent::__construct();
}
}
class C
{
public function display()
{
echo $this->test;
}
}
$b = new B();
$b->test = 1;
$b->c->display();
Your problem is here:
$class->test = $this->test;
You are attempting to use a property that is not yet defined, because when you do this:
$b->test = 1;
the constructor has already been called, and there's nothing in your classes to update C with the value of B's test property.
You can solve this in a couple of different ways.
1) Send the value in B's constructor, and pass it down the entire chain:
class A
{
public function __construct($test)
{
$this->load($test);
}
public function load($test)
{
$class = new C();
$class->test = $test;
$this->c = $class;
}
}
class B extends A
{
public function __construct($test)
{
parent::__construct($test);
}
}
class C
{
public function display()
{
echo $this->test;
}
}
$b = new B(123);
$b->c->display();
2) Add a method to B that will update C's property:
<?php
class A
{
public function __construct()
{
$this->load();
}
public function load()
{
$class = new C();
$this->c = $class;
}
}
class B extends A
{
public function __construct()
{
parent::__construct();
}
public function setTest($test)
{
$this->c->test = $test;
}
}
class C
{
public function display()
{
echo $this->test;
}
}
$b = new B();
$b->setTest(123);
$b->c->display();
Or perhaps a combination of both.

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