Jump to content

Class in class

- - - - -

  • Please log in to reply
11 replies to this topic

#1
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Hi!

I have class ClassA which load class ClassB. It looks like this:


ClassA.php

class ClassA {



	public function __construct($file)

	{

		require_once($file);

	}

	


}


$classa = new ClassA('ClassB.php');



ClassB.php

class ClassB {



	public function echoSome()

	{

		echo 'Some!';

	}

	


}



How can I use method from ClassB in ClassA? I`m doing something like framework and I need this information to done FrontController, because I have to load property method from classB.

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
What your ClassA (taken from your example) does is better done with __autoload() function in php. To access Class B methods, you need to instanciate an object of type ClassB and then call the methods.
$objb = new ClassB();

$objb->someMethodInB();

__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
I'm not quite sure if this is what you mean to do but you might just want to extend ClassA with ClassB. E.g.:


class ClassB { 



    public function echoSome() 

    { 

        echo 'Some!'; 

    } 

     


}  


class ClassA extends ClassB { 



    public function __construct() { /* ... */ } 

     


} 


Now all methods of classB may be used inside classA ( as long as their type allows it (public or protected) ).

EDIT: nvm, I think I misunderstood the purpose ;D.

#4
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
I have something like it:


class FrontController {

     /* some code of this class */


     /* method loads other controller */


public function loadController($name)

{


if(file_exists(APPLICATION_PATH . '/controllers/' . $name) {

require_once(APPLICATION_PATH . '/controllers/' . $name);

/* thanks it require_once function I can use methods from FrontController class in my controllers */


/* hear I want use method _init() from controller called $name */

/* Controller look like class with _init() instead of __construct(). In _init() method user initializes some actions which must be executed by default */

/* And I don`t know how to do it :p */

}


}

}



#5
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
The most easy way, to deal with several classes init.

$class = "ClassB";

$controller = new $class();

$controller->init();

But i believe, that a better way exist, and i just don't know it yet.)

#6
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
But when I want use method which name is in variable?

#7
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
If i understand correctly - you can do that too.

$method = "init";

$class->$method();

//make sure to read about __call magic method

but i really don't understand for what you need this.
You can do standart init() method call and define this method in interface - which every needable classes will implement.

#8
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
But what I have to do when I want type:


$model = new $nameOfSomeClass();


It is not working :(

#9
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
the $nameOfSomeClass should contain the name of the class you want to initiate. it's an variable.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#10
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
Checked now, it's work perfectly for me, but i am not sure if it's work in php4.. i always use 5.
Maybe you have a 4-th php or just doing it wrong.. can you share the problem code?
Here is how i checked
Class Test

{

    private $text;

    public function __construct()

    {

        $this->text = 'HEY!';

    }

    public function show()

    {

        echo($this->text);

    }

}



$class = "Test";

$test = new $class();

$test->show();
Hope this will help.

#11
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
All are working :)

Code:

class ClassA

{

	private $class;

	

	function __construct($class)

	{

		$this->class = $class;

	}

	

	function loadClass()

	{

		return new $this->class();

	}

}


class Test

{

    private $text;

    public function __construct()

    {

        $this->text = 'HEY!';

    }

    public function show()

    {

        echo($this->text);

    }

}


$class = new ClassA('Test');

$class->loadClass()->show();



#12
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
glad to hear that.)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users