I have two class called them class A and B. I created the 'A' class. And in this i create a 'B' class. How can i access the 'A' class variable from 'B' class?
class A
{
var letter;
var writers;
function __construct()
{
$this-letter = 'SOMETHING';
$this->writers = new B;
}
}
class B extends Writers
{
function __construct()
{
parent::__construct();
echo $letter; //This is where i want to acces outside variable (CLASS 'A')
}
}
I hope i was clear. I'm just rookie on OOP-ing. Please help me.
You can't, because $letter in A is was not declared public and B doesn't extend A.
If you don't want to expose A's data (one of the important OOP principles), you should use encapsulation. Create a getter in the A class
public function getLetter()
{
return $this->letter;
}
And then in B's construct method, create an instance of A and use said getter
$a = new A();
$letter = $a->getLetter();
In case you really need to use this structure, I guess you could do something like this:
class A
{
var letter;
var writers;
function __construct()
{
$this-letter = 'SOMETHING';
$this->writers = new B($this);
}
}
class B extends Writers
{
var $a;
function __construct(A $a)
{
parent::__construct();
$this->a = $a;
echo $this->a->letter;
}
}
This way, B would hold a reference to the A object it was created by. However, I rather recommend you to change your class topology, if possible.
Your B class needs to somehow have a reference to an A object. You could simply add to B :
class B extends Writers
{
private $a;
function __construct()
{
parent::__construct();
echo $letter; //This is where i want to acces outside variable (CLASS 'A')
}
public function A($a)
{
$this->a = $a;
}
}
Next, simply have your A object give a reference to itself to its B object.
class A
{
var letter;
var writers;
function __construct()
{
$this-letter = 'SOMETHING';
$this->writers = new B;
$this->writers->A($this);
}
}
The alternative would be static method.
It's hard to tell exactly what you want, but as I interpreted it:
class A extends Writers
{
var letter;
var writers;
function __construct()
{
$this-letter = 'SOMETHING';
$this->writers = new B;
}
}
class B extends A
{
function __construct()
{
parent::__construct();
echo $this->letter; //This is where i want to acces outside variable (CLASS 'A')
}
}
You can use static variable. like this:
class A
{
static $letter;
function __construct()
{
self::$letter = 'SOMETHING';
}
}
$objA = new A();
class B
{
function __construct()
{
$letter = A::$letter;
echo $letter; //print SOMETHING
}
}
Related
<?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();
Whats wrong with me OOP here.
I want to inherit from Class A
The return_output method will do something common so I don't want to write that in the inherited classes.
However when I do B->return_output() I want it to run the do_something method in Class B, but I see that it always runs the method from Class A.
Should I replace $this with something else?
class A {
private function do_something() {
// do something
}
public function return_output() {
$op = $this->do_something();
// add some wrappers to $op
return $op;
}
}
class B extends A {
private function do_something() {
// do something different
}
}
var newClass = new B;
echo B->return_output();
use protected and not private since you are running it inside of scope a and scope b can't access private scope a:
class A {
protected function do_something() {
echo('ado_something');
}
public function return_output() {
$op = $this->do_something();
// add some wrappers to $op
return $op;
}
}
class B extends A {
protected function do_something() {
echo('bdo_something');
}
}
$newClass = new B;
echo $newClass->return_output();
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();
is it possible to easily and quickly "assigning properties of one object to another"
class a {
public $number_one;
public $number_two;
public $number_three;
function __contruct() {
//do stuff
}
}
class b {
public $my_var;
function __contruct() {
$instanc_a = new a();
extract( $instance ); // but make these extracted object properties of class b????
// how? :-(
echo $this->number_one;
}
}
You can use get_object_vars to copy the public (only) properties of class a to the current object:
class b {
public $my_var;
function __construct() {
$instanc_a = new a();
$vars = get_object_vars($instanc_a);
foreach($vars as $name => $value) {
$this->$name = $value;
}
echo $this->number_one;
}
}
See it in action.
Note: You have a typo in your code (two cases of "contruct" instead of "construct") which will prevent things from working as they should.
Sounds like you actually want class b to extend class a
class b extends a {
public $my_var;
function __construct () {
parent::__construct();
// Now $this refers to anything in class b, or if it doesn't exist here, looks to class a for it
echo $this->number_one;
}
}
I have a class which initiates another class, i'm not concerned with having a reference to the object i only need the method and have to pass in new parameters.
class A {
__set .....
}
class B extends A {
$anotherA = new A;
$anotherA->myName = 'stackoverflow';
}
in short i'd like to have class B extend A, init a new instance of A but i don't want to have to type "new" everytime, i've seen the following syntax:
B::A // something like that
but not sure if how to use it or if that would do what i'm trying to do?
What you could do is define a static method on the class that returns the new instance. It's basically a 'shortcut', but it does exactly the same in the background.
class C {
public static function instance()
{
return new C();
}
public function instanceMethod()
{
echo 'Hello World!';
}
}
Now you can call it like:
C::instance()->instanceMethod();
Here are some examples of static functions - they can be called without using 'new A' or 'new B'.
class A {
static function message($msg = 'I am Alpha') {
echo "hi there, $msg\n";
}
}
class B {
static function message() {
A::message("I am Beta");
}
}
A::message();
B::message();
I would create the instance of A in B's constructor, then you can instantiate B using either its constructor or static B::create(), which just acts as a shortcut. You could make the constructor private if you wanted all instantiation go through create().
class A {
// __set .....
}
class B extends A {
public function __construct() {
parent::__construct();
$anotherA = new A;
$anotherA->myName = 'stackoverflow';
}
public static function create() {
return new self();
}
}
new B();
B::create();
Since you are extending A in B, you could call the method of class A:
class B extends A {
public function someMethod() {
parent::someMethodName();
}
}
Alternatively, you could create a static method in the class:
class A {
public static function someStaticMethod() { ... }
}
A::someStaticMethod();
If you really want a new instance of A, you have to use the new operator. That's what it is for.