Get property from another class function? - php

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

Related

How can I cast class for my object for PHP?

I have object $obj (class A).
Can I convert class for $obj to B?
Perhaps there is another way.
Example:
class A
{
public $AProp = 1;
public function &Convert($ATypeName)
{
// HOW?
return $this;
}
}
class B extends A
{
public $BProp = 2;
}
$obj=new A();
$obj->Convert("B");
print_r($obj->BProp);
I wrote next solution, but it is no good.
(It looks like your post is mostly code; I add some more details)
class A
{
public $AProp = 1;
public function &Convert($ATypeName)
{
$Result = new $ATypeName; // Create new object
$Result->AProp = $this->AProp; // Copy params...
return $Result;
}
}
class B extends A
{
public $BProp = 2;
}
$obj = new A();
$obj->AProp = 3;
$obj = $obj->Convert("B");
print_r($obj);
u are trying to use c++-way of class extending.
the php-way is smth like this:
<?php
class A
{
protected $prop = 1;
public function getProp()
{
return $this->prop;
}
}
class B extends A
{
protected $prop = 2;
}
$obj=new A();
print_r($obj->getProp());
$obj=new B();
print_r($obj->getProp());
also take a look on late static bindings - http://php.net/manual/en/language.oop5.late-static-bindings.php
<?php
class A
{
public static $prop = 1;
public function getProp()
{
return static::$prop;
}
}
class B extends A
{
public static $prop = 2;
}
$obj=new A();
print_r($obj->getProp());
$obj=new B();
print_r($obj->getProp());
This is the only way:
$obj = new B();
Since inheritance is used you can access all class A func and var, like $this->Aprop

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
}
}

object used as property guess the class of his owner

I was wondering if it is possible for an object to guess the class of his "owner" if the "owner" have a property of the object class.
Let me explain what I mean by a small example
class A {
public function Magic(){/*return owner's class*/}
}
class B {
private $foo;
public function __construct() {
$this->foo=new A();
}
public function getFoo(){
return $this->foo;
}
}
class C {
private $bar;
public function __construct() {
$this->bar=new A();
}
public function getBar(){
return $this->bar;
}
}
$b= new B();
$c= new C();
print($b->getFoo()->Magic()); // would print B
print($c->getBar()->Magic()); // would print C
I don't know if I am dreaming or if it's possible...
How would you do it if not possible?
You need to inject to A his owner. So, the Magic method is less "magical", but the dependency is more clear.
class A
{
private $owner;
public function __construct($owner)
{
$this->owner = $owner;
}
public function Magic() { return get_class($this->owner); }
}
class B
{
private $foo;
public function __construct()
{
$this->foo = new A($this);
}
public function getFoo()
{
return $this->foo;
}
}
class C
{
private $bar;
public function __construct()
{
$this->bar = new A($this);
}
public function getBar()
{
return $this->bar;
}
}
$b= new B();
$c= new C();
print($b->getFoo()->Magic()); // would print B
print($c->getBar()->Magic()); // would print C

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.

Cannot create object in class PHP

I try to create object in PHP class, but i get some interesting errors in IDE, like unexpected ( token etc. Here is my code:
class A {
public $a = 1;
}
class B {
$aa = new A();
}
Where is the problem?
In PHP, you can only assign "fixed" values to properties in the class definition.
class A {
public $a = 3; // will work
public $b = "hello"; // will work
public $c = foo(); // won't work
public $d = new Foo(); // won't work
}
If you want to do so, you can use the __construct() method which will be called every time a new instance is created or any other method that you call.
class B {
public $aa; // define visibility of $aa
function __construct() {
$this->aa = new A();
}
}
You need to make a constructor on class A
class A {
function __construct() {
$this->a = 1;
}
public function returnA() {
return $this->a;
}
}
$aa = new A();
echo $aa->returnA();
Try to create a constructor in class A and see if it works:
class A {
public $a;
function __construct()
{
$this->$a = 1;
}
}
class B {
$aa = new A();
}

Categories