Jump to content


calling a class function in a class function

  • Please log in to reply
2 replies to this topic

#1 brown2005


    Advanced Member

  • Members
  • PipPipPip
  • 943 posts

Posted 07 February 2013 - 10:41 PM

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?

#2 Hall of Famer

Hall of Famer

    OOP Fanboi

  • Members
  • PipPipPip
  • 315 posts
  • LocationIthaca

Posted 07 February 2013 - 11: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:

class Class1{
	 public function a();
	 public function c(){
class Class2{
	 public function b(){
		 $class1 = new Class1();

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 - 11: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 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 08 February 2013 - 12:31 AM

Better to use dependency injection, and avoid the interdependence between classes:
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