Jump to content

using one class method inside another class... Not sure on this way I have...


Recommended Posts

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

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 ;)

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

 

 

 

 

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 :)

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.