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