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();
Related
Can I do this?
class A {
public function foo() { echo "whatever";}
}
class B {
static public $var;
static function initVar($var) { self::$var = $var; }
static public function bar() { return self::$var->foo(); }
}
class C {
public function baz() {
$a = new A();
B::initVar($a);
echo B::bar(); // should print "whatever"
}
}
if not, is there any way to a static method to access an given object instance ?
Yes, you can. This is usually how Singleton (a pattern where a object only should be instantiated once) is implemented. Though this pattern is considered bad...
http://en.wikipedia.org/wiki/Singleton_pattern
Example (though within the same class...):
class A
{
private static $inst;
public static function instance()
{
if (self::$inst === NULL)
{
self::$inst = new A();
}
return self::$inst;
}
/* constructor etc */
}
$a = A::instance();
$a->someMethod();
?>
So say I have the following code,
$obj = new foo();
echo $obj;
class foo {
public function __construct()
{
return 'a';
}
}
How do I make $obj echo the string 'a'?
How do I make $obj refer to or equal what is returned by the object/class?
Need to return a value from a __construct(), and also a normal private function within another class. For example:
$obj2 = new foo2();
echo $obj2;
class foo2 {
public function __construct()
{
bar();
}
private bar()
{
return 'a';
}
}
Thanks!
you can use the magic __toString() method to convert your class to a representing string.
You should not return something in your constructor, __toString() is automaticly called if you try to use your instance as string (in case of echo).
from php.net:
<?php
// Declare a simple class
class TestClass
{
public $foo;
public function __construct($foo)
{
$this->foo = $foo;
}
public function __toString()
{
return $this->foo;
}
}
$class = new TestClass('Hello');
echo $class;
?>
http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
Constructors in PHP are more like initialisation functions; their return value is not used, unlike JavaScript for instance.
If you want to change the way objects are normally echoed you need to provide the magic __toString() method:
class foo
{
private $value;
public function __construct()
{
$this->value = 'a';
}
public function __toString()
{
return $this->value;
}
}
A private method that would return the value can be used in a similar manner:
class foo2
{
private function bar()
{
return 'a';
}
public function __toString()
{
return $this->bar();
}
}
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();
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.
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);
}
}