SystemOverload Posted September 7, 2011 Share Posted September 7, 2011 Hi Can you call Class A's methods or properties from Class B's methods? Thanks. Link to comment https://forums.phpfreaks.com/topic/246662-calling-class-properties-methods-from-a-different-class-method/ Share on other sites More sharing options...
Maq Posted September 7, 2011 Share Posted September 7, 2011 Check out - http://us2.php.net/manual/en/language.oop5.visibility.php Link to comment https://forums.phpfreaks.com/topic/246662-calling-class-properties-methods-from-a-different-class-method/#findComment-1266581 Share on other sites More sharing options...
SystemOverload Posted September 7, 2011 Author Share Posted September 7, 2011 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? Link to comment https://forums.phpfreaks.com/topic/246662-calling-class-properties-methods-from-a-different-class-method/#findComment-1266587 Share on other sites More sharing options...
KevinM1 Posted September 8, 2011 Share Posted September 8, 2011 The answer is a qualified 'yes', depending on how you've set things up. If you want help, show your code rather than talk in vague generalizations. Link to comment https://forums.phpfreaks.com/topic/246662-calling-class-properties-methods-from-a-different-class-method/#findComment-1266673 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 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'); } } ?> Link to comment https://forums.phpfreaks.com/topic/246662-calling-class-properties-methods-from-a-different-class-method/#findComment-1266709 Share on other sites More sharing options...
SystemOverload Posted September 8, 2011 Author Share Posted September 8, 2011 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 Link to comment https://forums.phpfreaks.com/topic/246662-calling-class-properties-methods-from-a-different-class-method/#findComment-1267107 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 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; } } ?> Link to comment https://forums.phpfreaks.com/topic/246662-calling-class-properties-methods-from-a-different-class-method/#findComment-1267116 Share on other sites More sharing options...
SystemOverload Posted September 11, 2011 Author Share Posted September 11, 2011 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". Link to comment https://forums.phpfreaks.com/topic/246662-calling-class-properties-methods-from-a-different-class-method/#findComment-1268053 Share on other sites More sharing options...
xyph Posted September 11, 2011 Share Posted September 11, 2011 Didn't seem to work? What does that mean? My examples work flawlessly. Regardless, glad you found your solution through inheritance. Link to comment https://forums.phpfreaks.com/topic/246662-calling-class-properties-methods-from-a-different-class-method/#findComment-1268060 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.