Jump to content

[SOLVED] Problem in calling method from a class within a class


NoUser2U

Recommended Posts

Hi,I have a class that has a method which initiates other classes. (problem explanation is below the next snippets of code)Index.php

<?phpsession_start();class FrontController{	// properties	/* private $view	 * 				the view that'll be inserted/called	 * 	 * 	 * private $frontc_id	 * 				ID for METHOD -> which is the selected date from calendarView.php into dayView.php	 * 	 */	private $controller;	private $view;	private $frontc_id;			private $object;		// methods	/*Instantiator	 * 	 */	public static function instantiator()	{		$instance = new self();		return $instance;	}			/*Timezone set	 * 	 */	public function __construct()	{		// SET TIMEZONE		date_default_timezone_set('Europe/Amsterdam');	}			/* HANDLER	 * 	 * Dispatcher 	 * 	 */	public function dispatch()	{		//localhost/index.php?controller=controller_for_view&method=method_for_controller (this is how my URL's look like)				// catch Controllername		$controller_catcher = (!empty($_GET['controller'])) ? $_GET['controller'] : 'home';		if(!isset($_SESSION['loggedin'])) { $controller_catcher = 'register'; }		$this->controller = $controller_catcher . 'Controller';				// define View		$this->view = VIEW_DIR . $controller_catcher . 'View.php';		if(!file_exists($this->view)): die('Page not found, can not find view'); endif;				// include Controller		$file = CONTR_DIR . $this->controller . '.php';		if(!file_exists($file)): die('Page not found, can not find controller'); endif;		require_once($file);		// Instantiate Controller 		$this->object = new $this->controller;				// Catch Method		if(!empty($_GET['method'])) 		{ 			$method = 'do' . ucfirst($_GET['method']);			// only if method is not empty, do that!		        // Catch ID's for Method			if(empty($_GET['id'])) : 					$this->object -> $method();  				elseif(!empty($_GET['id'])) :					$frontc_id = $_GET['id']; 					$this->object -> $method($frontc_id);					endif;		}		// include View 		// CHECK FOR LOGGED IN, SEE ALSO DISPATCHER-> catch controller		if(!isset($_SESSION['loggedin'])) : 				require_once(VIEW_DIR . 'config/mainframe2.php');			else :				require_once(VIEW_DIR . 'config/mainframe.php');			endif;			}}?>

Some controller: 'searchresultController.php'

<?php// searchresultController.phpclass searchresultController extends Controller{	// properties		// methods	public function doResult($input=null)	{				echo searchresultController::doAddfriend();		// THIS IS MY CONCERN, i cannot call this using '$this->doAddfriend()'		}		private function doAddfriend()	{		if(isset($_POST['addfriend']))		{			return $_POST['addfriend'];		}	}	}?>

Basic program flow:Now on the index page the Frontcontroller class gets instantiated and the method 'dispatcher' within that class will be called. The dispatcher method takes parts off of the URL to determine which controller (other classes) and method (from those other classes) to call.So basically, i have a method (dispatcher) in a class (frontcontroller) that instantiates other classes (here: searchresultController). Now inside these other classes i have ofcourse methods and sometimes, i need to call a method from within another method of a controller.In the above 'searchresultController.php' class, i need to call the method doAddfriend() (see: searchresultController.php in method: doResult()). Remember though, an object of the class 'searchresultController' was instantiated by the method 'dispatch()' in the Frontcontroller class.Normally, i call methods using $this->method(). But if i use that in this example, i get an error saying:Fatal error: Call to undefined method FrontController::doAddfriend() in /path/to/my/project/searchresultController.php on line 23 So the only way for me to call these methods is to use controllername::methodname (or self::methodname, which also works), but i cannot use $this->methodname();I am sure the problem lies in the Frontcontroller class, which somehow does not allow me to use '$this->' in other classes (i think it's because i instantiate classes from inside the dispatcher method of the Frontcontroller class).Can somebody please check out the structure and tell me what to change in order for me to use $this->methodname to call methods?

Link to comment
Share on other sites

How are you calling doAddFriend in you code? Using :: means to call it statically so no instance of that object exists when you run that function. You would only be able to use $this-> if the object has been constructed.

Fatal error: Call to undefined method FrontController::doAddfriend()

But I don't see the relation to this in your posted code since searchresultController does not extend FrontController directly and FrontController does not extend anything. I'm not sure if it's relevant but maybe post your Controller class code if you still have issues. Also the page where you construct your objects and call methods.

Link to comment
Share on other sites

How are you calling doAddFriend in you code? Using :: means to call it statically so no instance of that object exists when you run that function. You would only be able to use $this-> if the object has been constructed.
Fatal error: Call to undefined method FrontController::doAddfriend()

But I don't see the relation to this in your posted code since searchresultController does not extend FrontController directly and FrontController does not extend anything. I'm not sure if it's relevant but maybe post your Controller class code if you still have issues. Also the page where you construct your objects and call methods.

Thank you for your post MrFish (nice nickname btw :)). I managed to solve my problem, which was actually pretty simple:The Frontcontroller class get's instantiated on the index page, and it calls the Dispatch() method (see Frontcontroller.php and index.php). Now in the Dispatch() method, a class for a controller (requested from the URL) get's instantiated with the line:
// Instantiate Controller $this->object = new $this->controller;

So in order for me to call a method INSIDE a view (which is pure html), i have to use:

$this->object -> methodname();

My problem was that i had forgotten that a page controller get's instantiated automatically by the Frontcontroller, and all i had to do was use $this->object -> methodname() to call a method of the page controller. Instead, i used controllername::methodname(); which give me the problem of not being able to use '$this' in a method of a page controller.It's a bit confusing, but all in all it was something very straightforward.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...