Jump to content

Calling class properties / methods from a different class' method?


Recommended Posts

Thanks, but that didn't help.  The properties are public, or at least some are, but the object is not recognised:

 

Notice: Undefined variable: objSession in C:\WEBSERVER\system\classes\clAccount.class on line 9 Notice: Trying to get property of non-object in C:\WEBSERVER\system\classes\clAccount.class on line 9 

 

Can you give me a yes/no?  Should I be able to access Class A's properties and methods from Class B's methods?

This might help you understand how you can have two classes interact with each other. This doesn't touch on inheritance, which can be read about here: http://php.net/manual/en/language.oop5.inheritance.php

 

<?php 

$obj = new b();

echo $obj->callToA();
echo '<br>';

$obj2 = new a();

echo $obj->thisNeedsA( $obj2 );

class a {

public $property = 'hello world';

public function method( $argument ) {
	return $this->property . ', ' . $argument;
}

}

class b {

public $object;

public function __construct() {
	$this->object = new a();
}

public function callToA() {
	return $this->object->method('i love you');
}

public function thisNeedsA( a $obj ) {
	$obj->method('you love me');
}

}

?>

Apologies, I thought I'd been quite clear in my question.  Let me layout exactly what I want to know is possible or not:

 

class clClassA {

public varA;

function clClassA {
...blah...
$this->varA = 999;
...blah...
}

function fnFunctionA {
<does something else>
}

}


class clClassB {

function clClassB {
echo $objA::varA;
}
}


objA = new clClassA;
objB = new clClassB; 

 

 

I want objB to be able to access objA::varA or execute objA::fnFunctionA

 

IE: access one class' properties/methods from within another class' methods.  I do not want to instigate things or access properties from outside of the classes.

 

Is my question clearer now?

 

Thnx

Only if you use static methods, which I don't suggest in this case.

 

My examples touched on exactly how you can do this.

<?php 

// METHOD ONE
$obj = new static_b();
echo $obj->get_varA();

class static_a {
public static $varA = 'hello world';
}
class static_b {
public function get_varA() {
	return static_a::$varA;
}
}

// METHOD TWO (BETTER IMO)
$obj = new object_b();
echo $obj->get_varA();

class object_a {
public $varA = 'hello world';
}
class object_b {
public function get_varA() {
	$obj = new object_a();
	return $obj->varA;
}
}

// METHOD THREE (YOU DON'T LIKE, BUT HAS IT'S PLACE)
$objA = new passing_a();
$objB = new passing_b();
echo $objB->get_varA( $objA );

class passing_a {
public $varA = 'hello world';
}
class passing_b {
public function get_varA( passing_a $obj ) {
	return $obj->varA;
}
}

?>

1 and 2 didn't seem to work and I didn't like the look of 3, so steered clear. 

 

I went with extending ClassB from ClassA.  Instantiated ClassB, which had a constructor that called ClassA's constructor.  This then allowed me to call ClassA's variable from inside CLassB as "$this->variable".

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.