rwwd Posted October 17, 2010 Share Posted October 17, 2010 Hi people! class FirstOne{ public function FunctionOne($FirstInput){ //do stuff and output value return $value1; } } Then:- class SecondOne{ public function FunctionTwo($AnotherInput){ //do stuff and output value return $value2; } } What I want to know is this, if I want to use FunctionOne() in Class SecondOne do I do it like this:- (Assume as I have instantiated the first class using $Test = new FirstOne(); ) class SecondOne{ function SecondedFunction(){ global $Test; return $Test->FunctionOne(); } public function FunctionTwo($AnotherInput){ //do stuff and output value return $value2; } public function FunctionThree(){ //some code here $this->Test->SecondedFunction();<--I think as I can omit the $this-> reference } } My point is: Do I have to do it this way or is there way of having this done through __construct() that would negate the need for a third party function? I have a version working, I just think that it is a little convoluted in the way as I have done it, so I thought I would ask you guys. Any help/advice is appreciated. Cheers Rw Quote Link to comment https://forums.phpfreaks.com/topic/216085-using-one-class-method-inside-another-class-not-sure-on-this-way-i-have/ Share on other sites More sharing options...
ignace Posted October 17, 2010 Share Posted October 17, 2010 Always stick to proper OO-design, if something doesn't work for some reason then you need to re-consider your design not hack it Quote Link to comment https://forums.phpfreaks.com/topic/216085-using-one-class-method-inside-another-class-not-sure-on-this-way-i-have/#findComment-1123080 Share on other sites More sharing options...
rwwd Posted October 17, 2010 Author Share Posted October 17, 2010 This is considered hacking!? I have a method in one class that I simply want to make available in other classes, if this is considered a hack then I hadn't realised it... Rw Quote Link to comment https://forums.phpfreaks.com/topic/216085-using-one-class-method-inside-another-class-not-sure-on-this-way-i-have/#findComment-1123083 Share on other sites More sharing options...
ignace Posted October 18, 2010 Share Posted October 18, 2010 This is considered hacking!? I have a method in one class that I simply want to make available in other classes You used a global to pass an object while you should use the constructor or a setMethod() (just don't over-use the latter) class A { function sayHello($b) { echo 'Hello, ', get_class($b), '. How are you doing today?'; } } class B { function __construct(A $a) { $this->a = $a; } function greetings() { $this->a->sayHello($b); $this->sayHello($this->a); } function sayHello($a) { echo 'Fine, thanks for asking. And how are you doing on this sunny day ', get_class($a), '?'; } } $a = new A(); $b = new B($a); $b->greetings(); You could also pass $a on greetings() making it Lazy-Loaded Quote Link to comment https://forums.phpfreaks.com/topic/216085-using-one-class-method-inside-another-class-not-sure-on-this-way-i-have/#findComment-1123302 Share on other sites More sharing options...
rwwd Posted October 18, 2010 Author Share Posted October 18, 2010 Hi there Ignance, Well, thanks for the tips there; this really just tells me that I really need to spend more time understanding the working intricacies of classes before I actually try to 'improve' something that I did a while ago. I shall *try* to implement your advice as what you have written is JUST what I was after, and it's amazing how close I actually got to how you have done the example. I know about using __construct() as I find it invaluable as a way of importing settings and control arrays (without the need of 'global') and config files that are needed during the application running. WRT: setMethod() this is a new one on me, I shall have to consult the manual to see what that is all about, I am however, intrigued as to why you say don't over use.. I guess I shall have to find out! Thanks. Rw Quote Link to comment https://forums.phpfreaks.com/topic/216085-using-one-class-method-inside-another-class-not-sure-on-this-way-i-have/#findComment-1123312 Share on other sites More sharing options...
ignace Posted October 18, 2010 Share Posted October 18, 2010 By setMethod() is actually meant setter-methods aka mutator-methods. The reason you should not over-use them is because OO is about encapsulation if you create a set-method for each class variable, then it isn't encapsulated (shielded from the outside world) at all Quote Link to comment https://forums.phpfreaks.com/topic/216085-using-one-class-method-inside-another-class-not-sure-on-this-way-i-have/#findComment-1123333 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.