Access Class in Class Variable - php

I have this code:
class A
{
public $db
}
class B
{
public $cssA
public function __construct()
{
$this->cssA = new A();
}
}
The question is, how can I call a method in class B from class A?

You can't as there is no reference to the object of class B.
class A {
public $db;
private $b;
public function __construct(B $b) {
$this->b = $b;
}
}
class B {
private $a;
public function __construct() {
$this->a = new A($this);
}
}
Methods of object of class B can be now accessed through $this->b->doSomething() within object of class A.

You would have to instantiate class b within class a and then call the method...
$this->aProp = new A();
$this->aProp->classAfunction();
$aVal = $this->aProp->publicProperty;
Basic oo programming stuff.

Related

Multiple inheritance + Multi-Level inheritance in PHP: Is there a pattern or possibility to solve constructor inheritance?

Have a look at the following example:
class A {
protected $a;
public function __construct() {
$this->a = "foo";
}
}
trait Q {
protected $q;
public function __construct() {
$this->q = "happy";
}
}
class B extends A {
use Q;
protected $b;
public function __construct() {
$this->b = "bar";
}
}
trait X {
protected $x;
public function __construct() {
$this->x = "lorem";
}
}
class C extends B {
use X;
protected $c;
public function __construct() {
$this->c = "sure";
}
public function giveMeEverything() {
echo $this->a." ".$this->b." ".$this->c." ".$this->x." ".$this->q;
}
}
$c = new C();
$c->giveMeEverything();
This works just fine - the output is:
sure
The thing is that I want all classes and and traits within the tree to initialize their member variables. Desired output:
foobarsureloremhappy
It must not be solved with constructors! I just want the member variables to be populated on initialization, but I still had no good idea how to solve this. In a real world example this is more complex, therefore please do not $a = "foo"; just within the variables declaration.
The problem is that traits cannot be instantiated so __construct() is kind of meaningless.
The best approach is to initialize your member variables using the class constructor; that's why constructors exist.
If you want to initialize some members that are declared in a trait, then have a trait function and call it in the appropriate class constructor, example:
trait Q {
protected $a;
public function initQ() { $this->a = "whatever"; }
}
class MyClass {
use Q;
public function __construct() {
$this->initQ();
}
}

How to make an abstract variable static to a sub class

<?php
abstract class A {
public static $var = "A";
public function setVar() {
}
public function test() {
$this->setVar();
echo static::$var;
}
public function returnVar() {
return static::$var;
}
}
class B extends A {
public function setVar() {
static::$var = 'B';
}
}
class C extends A {
public function setVar() {
static::$var = 'C';
}
}
$class = new B();
$class->test();
$class2 = new C();
$class2->test();
echo "</br>";
echo $class->returnVar();
echo $class2->returnVar();
?>
What I'm trying to do is make the variable $var static to the class that extends abstract class A without having to re-declare it else where.
So say perhaps I create multiple objects from class B that extends A, I want all objects made from class B to share the same $var value.
Say I then create objects based on class C, they should all share the same value of $var...
This is the result I'm currently getting:
BC
CC
However, what I'm looking for is:
BC
BC
Thanks
Try it like that:
#setting
public function setVar() {
static::$var[get_class($this)] = 'B';
}
#getting in abstract
public function returnVar() {
return static::$var[get_class($this)];
}
#add this in the abstract class
public function setVar();

Call object method from child object(PHP)

Say object of class B is attribute of class A. How can I call method of object of class A from method of object of class B? What would be nice solution without passing object link?
Thanks!
Here goes code sample:
class A{
var $b;
function __construct(){
$this->b = new B();
}
function f1(){
$this->b->f3();
}
function f2(){
echo 'hello!';
}
}
class B{
function f3(){
// call f2() method in object $obj(not new A())
}
}
$obj = new A();
$obj->f1();
You can use a static function
public static function f2{
echo 'hello!';
}
with f3 defined as
function f3(){
A::f2();
}
This may not ultimately be the solution you want, however. See more info here.
The only way you can access that instance's function is if you inject it on the B object as a dependency. You can inject it within the constructor, like this:
<?php
class A {
protected $b;
public function __construct() {
$this->b = new B($this);
}
public function f1() {
$this->b->f3();
}
public function f2() {
echo 'hello!';
}
}
class B {
protected $a;
public function __construct($a) {
$this->a = $a;
}
public function f3() {
$this->a->f2();
}
}
$obj = new A();
$obj->f1();

How to call a non-static method from another class

This's my second question, even thought, i answered the previous one, on my own. Anyway, I have a basic problem with OOP, on how to call a non-static method from another class. example:
We have a class named A in a file A.class.php
class A {
public function doSomething(){
//doing something.
}
}
and a second class named B on another file B.class.php
require_once 'A.class.php';
class B {
//Call the method doSomething() from the class A.
}
I think now it's clearn. How to : Call the method doSomething() from the class A ?
Class B will need an object of Class A to call the method on:
class B {
public function doStuff() {
$a = new A();
$a->doSomething();
}
}
Alternatively, you can create the instance of A outside of B and pass it into B's constructor to create a global reference to it (or pass it to an individual method, your choice):
class B {
private $a = null;
public function __construct($a) {
$this->a = $a;
}
public function doStuff() {
$this->a->doSomething();
}
}
$a = new A();
$b = new B($a);
How about injecting class A into B, making B dependant on A. This is the most primitive form of dependency injection:
class A
{
public function doSomething()
{
//doing something.
}
}
class B
{
private $a;
public function __construct( A $a )
{
$this->a = $a;
}
//Call the method doSomething() from the class A.
public function SomeFunction()
{
$this->a->doSomething();
}
}
This is constructed like this:
$a = new A();
$b = new B( $a );
You need to instantiate a an object of class A. You can only do this inside a method of class B.
class B{
public function doSomethingWithA(){
$a = new A();
return $a->doSomething();
}
}
class B {
public function __construct()
{
$a = new A;
$a->doSomething();
}
}
I know this is an old question but considering I found it today I figured I'd add something to #newfurniturey's answer.
If you wish to retain access to class B within class A this is what I did:
class A
{
private $b = null
public function __construct()
{
$this->b = new B($this);
if (!is_object($this->b) {
$this->throwError('No B');
}
$this->doSomething();
}
public function doSomething() {
$this->b->doStuff();
}
private function throwError($msg = false) {
if (!$msg) { die('Error'); }
die($msg);
}
}
class B {
public function doStuff() {
// do stuff
}
}
This is constructed like this:
$a = new A();

Accessing class properties

How do i access the properties of class A from an object instantiated inside class A.
Like this;
class A()
public var1;
public obj1;
function __construct(){
$this->var1 = 'Hello World';
$this->obj1 = new B();
}
==============
class B()
function anything(){
#i want to access var1 from the calling class here ????
# how do i access var1 in the calling class
}
There's no direct way to do this. Dependency injection is a possibility:
class B {
protected $A = null;
public function __construct($A) {
$this->A = $A;
}
public function foo() {
$this->A->var1;
}
}
class A {
public function __construct() {
$this->obj1 = new B($this);
}
}

Categories