Jump to content

[another_question] Calling multiple methods/variables at the same time?


NoUser2U

Recommended Posts

Hi all,Since i'm quite a beginner with OOP, i have this line of code which i don't understand. Let's say you have a class:

class FrontController {  public static function createInstance()   {	// some code  }    public function dispatch()   {	// some more code  }}

Elsewhere, this class is instantiated with the following line:

FrontController::createInstance()->dispatch();

My question is: How should i understand what this last line of code actually does? I know Classname::methodname() instantiates a class Classname and calls a method Methodname, so is Classname::methodname()->anothermethodname() calling bóth methods, first methodname() then anothermethodname() ??Also, if my above assumption is true, does one of the methods affect the other in any way? Or is calling multiple methods like this nothing more than just a shortcut for:Classname::methodname();Classname::anothermethodname();Lastly, does this way of calling also work for variables/properties?Thanks in advance

Link to comment
Share on other sites

Using Classname::methodname() does instantiate the class, it calls a static method in the class itself. An object hasn't been created yet. If this syntax is used:FrontController::createInstance()->dispatch();Then that tells me that the createInstance method returns some object which has a dispatch method. The createInstance method might create a new instance of the FrontController class, but it might not (the code would tell you). That syntax would be the same as this:$obj = FrontController::createInstance();$obj->dispatch();

Link to comment
Share on other sites

Using Classname::methodname() does instantiate the class, it calls a static method in the class itself. An object hasn't been created yet. If this syntax is used:FrontController::createInstance()->dispatch();Then that tells me that the createInstance method returns some object which has a dispatch method. The createInstance method might create a new instance of the FrontController class,
Wow i feel so stupid right now hehe... when i just read that last sentence in the quoted text, I suddenly realized the method 'createInstance' in my class indeed creates a new instance of the FrontController class. I admit though that i got the code from some tutorial, and just typed everything over and quickly looked it over to see if i understood it. In general, i did, but obviously there were some details (and might still be) which i have overlooked. Thank you for your reply!BTW, the full code for the FrontController class was:
class FrontController {  public static function createInstance()   {	if (!defined("PAGE_DIR")) {	  exit("Critical error: Cannot proceed without PAGE_DIR.");	}		$instance = new self();	return $instance;  }    public function dispatch()   {	$page = !empty($_GET["page"]) ? $_GET["page"] : "home";	$action = !empty($_GET["action"]) ? $_GET["action"] : "index";	$class = ucfirst($page) . "Actions"; 					//e.g. HomeActions	$file = PAGE_DIR . "/" . $page . "/" . $class . ".php"; //e.g. pages/home/HomeActions.php	if (!is_file($file)) 	{	  exit("Page not found");	}		require_once $file;	$actionMethod = "do" . ucfirst($action);	$controller = new $class();		if (!method_exists($controller, $actionMethod)) 	{	  exit("Page not found");	}		//e.g. $controller->doIndex();	$controller->$actionMethod();	exit(0);  }}

I'm sorry for letting the code out in the first post, i thought it was irrelevant, but it holds the answer to my question obviously. Anyway, much thanks for your help justsomeguy! :)

Link to comment
Share on other sites

So i continued with the same tutorial, and now i have this block of code:

 public function dispatchAction($action) {	$actionMethod = "do" . ucfirst($action);	if (!method_exists($this, $actionMethod)) {	  exit("Page not found");	}	$this->$actionMethod();  }

What confuses me is the 3rd line, i.e. ''if (!method_exists($this, $actionMethod)) {". Now i'm familiar with using the $this keyword, as in $this->somevariabel_or_method for example:$this -> foo = 'bar' , where foo is some property of the class.But what's up with a $this keyword withoud the -> that usually follows it ? (like in the above example of code). In the above example it's used in a method_exists() function, where $actionMethod is some class or object. So i guess $this in that same line of code is some methodname of object/class $actionMethod...but to what is $this in that same line referring to? :S

Link to comment
Share on other sites

in OOP, $this is usually a reference to a class or object. It is a way to define scope. so in this case, it's checking if $actionMethod is a property of $this, which is the class.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...