benphelps Posted April 28, 2009 Share Posted April 28, 2009 Im trying to create a class for this project of mine. This is what it looks like now class myclass { function myfunction(){ } } I use this like I should $class = new myclass; $class->myfunction(); I want to make a subfunction of "myfunction", so I can use it like this. $class = new myclass; $class->myfunction->subfunction(); How would I go about doing that ? Quote Link to comment https://forums.phpfreaks.com/topic/155939-making-a-subfunction-in-a-class/ Share on other sites More sharing options...
premiso Posted April 28, 2009 Share Posted April 28, 2009 I think you want to look in extending the class. See that page for an example. Quote Link to comment https://forums.phpfreaks.com/topic/155939-making-a-subfunction-in-a-class/#findComment-820850 Share on other sites More sharing options...
benphelps Posted April 28, 2009 Author Share Posted April 28, 2009 No, then I would have to call it like this. $class = new myclass; $class->subfunction(); and not like this $class = new myclass; $class->myfunction->subfunction(); I have tried to do an extend class, and I cant call it through the function myfunction. Quote Link to comment https://forums.phpfreaks.com/topic/155939-making-a-subfunction-in-a-class/#findComment-820851 Share on other sites More sharing options...
Philip Posted April 28, 2009 Share Posted April 28, 2009 <?php error_reporting(E_ALL); class subClass { public $test; function __construct($test) { $this->test = $test; } public function subMethod( ) { echo $this->test; } } class mainClass { public $subClass; public $foo; function __construct( ) { $this->subClass = new subClass('hello'); } } $myClass = new mainClass; $myClass->subClass->subMethod(); ?> Its basically making the variable subClass a new class, which allows access to its methods. Whats the purpose for this? Quote Link to comment https://forums.phpfreaks.com/topic/155939-making-a-subfunction-in-a-class/#findComment-820861 Share on other sites More sharing options...
benphelps Posted April 28, 2009 Author Share Posted April 28, 2009 1. It looks really nice and easy to use for people who might not know alot about php. 2. I have alot of functions in the base class, and I want to extend them in categories. Like this: $administration->account->create($somevars); $administration->account->downgrade($somevars); $administration->account->ban($somevars); $administration->account->delete($somevars); $environment->announce->toplayer($somevars); $environment->announce->toworld($somevars); EDIT: Much thanks for your help, works perfect. Quote Link to comment https://forums.phpfreaks.com/topic/155939-making-a-subfunction-in-a-class/#findComment-820870 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.