class Function1 {
function a() { }
function c () { $this->a() }
}
class Function2 {
funcation b () { Function1->a(); }
}
is it possible to get rid of function c and just use b calling in function a?
calling a class function in a class function
Started by brown2005, Feb 07 2013 05:41 PM
2 replies to this topic
#1
Posted 07 February 2013 - 05:41 PM
#2
Posted 07 February 2013 - 06:07 PM
Yeah its possible, but the way you are using classes is somewhat weird. I dont think you grasp the concept of OOP nicely. Heres what you are supposed to do in class 2:
Or you can declare all methods to be static so you do not need to instantiate an object. It seems to me that you have no idea what a class does and how class methods(member functions) differ from procedural functions.
class Class1{
public function a();
public function c(){
$this->a();
}
}
class Class2{
public function b(){
$class1 = new Class1();
$class1->a();
}
}
Or you can declare all methods to be static so you do not need to instantiate an object. It seems to me that you have no idea what a class does and how class methods(member functions) differ from procedural functions.
Edited by Hall of Famer, 07 February 2013 - 06:09 PM.
Welcome to the world of OOPHP! In a perfect script, everything is an object. You cannot be perfect, but you can approach as close as can.

#3
Posted 07 February 2013 - 07:31 PM
Better to use dependency injection, and avoid the interdependence between classes:
You'll also note that I've removed the
class Class1 {
public function a() {
// Do something.
return 'test';
}
class Class2 {
private $data;
public function __construct (Class1 $dataStore) {
$this->data = $dataStore;
}
public function b(){
return $this->data->a();
}
}
// Create the objects
$objectA = new Class1 ();
$objectB = new Class2 ($obejctA);
// Print out the results from Class1::a() via the second class.
echo $objectB->b ();
You'll also note that I've removed the
c () method, as it wasn't doing anything, and added a body to the a () method.
Keeping it simple.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users












