I am having trouble calling a specific method from another class in my app. I have a class, Rest, that determines various settings, etc. about a particular request received by the server and creates a Rest object with the properties of the request. The Rest class may then call any given method in a separate class to fulfill the request. The problem is that the other class needs to call methods in the Rest class to send a response, etc. How can this be possible?
Here's a blueprint of my current setup: class Rest {
public $controller = null;
public $method = null;
public $accept = null;
public function __construct() {
// Determine the type of request, etc. and set properties
$this->controller = "Users";
$this->method = "index";
$this->accept = "json";
// Load the requested controller
$obj = new $this->controller;
call_user_func(array($obj, $this->method));
}
public function send_response($response) {
if ( $this->accept == "json" ) {
echo json_encode($response);
}
}
}
The controller class: class Users {
public static function index() {
// Do stuff
Rest::send_response($response_data);
}
}
This results in receiving a fatal error in the send_response method: Using $this when not in object context What's the better way to do this without sacrificing the current workflow. View Discussion Improve Article Save Article ReadDiscussView Discussion Improve Article Save Article Static methods The static method in PHP is same as other OOP languages. Static method should be used only when particular
data remains constant for the whole class. As an example, consider that some programmer is making the data of a college and in that every object needs getCollegeName function that returns the same college name for all objects as name of college then this function should be made static. Basically static methods are used when to access that method without the object of that class. Static method are used when there is a chance to use method without help of class
objects. Example: PHP<?php
class College
{
static function getCollegeName()
{
return "MNNIT Allahabad";
}
}
echo (College::getCollegeName());
?>
Instance Methods Instance method is used when there is no chance to call the method without existing of object. For example
consider a College class in which getPersonName() method which returns the name of person. This method should exist only when there is a particular type of person object. Example: php<?php
class College
{
private $name ;
function setName( $name ) {
$this -> name = $name ;
}
function
getName() {
return $this -> name;
}
}
$obj = new College;
$obj -> setName("Geeks");
echo ( $obj -> getName());
?>
Comparison between instance and static methods: - The static method can call without object while instance method can not be called without object.
- The execution time of instance method is faster than static
method but in the PHP version 5.3 there was a bug which says that static methods are faster in which they have introduced late binding.
Example: php<?php
set_time_limit(0);
echo 'Current PHP version: ' . phpversion();
Class St {
public static $count = 0;
public static function getResult()
{
self:: $count = self:: $count + 1;
}
}
$time_start_static = microtime(true);
for ( $i = 0; $i < 100000; $i ++) {
St::getResult();
}
$time_end_static = microtime(true);
$time_static = $time_end_static - $time_start_static ;
echo "\nTotal execution time is for static method is: '$time_static' ";
class Ins {
private $count = 0;
public function getResult() {
$this -> count = $this -> count + 2;
}
}
$ob = new Ins;
$time_start_instance = microtime(true);
for ( $i = 0; $i < 100000; $i ++)
{
$ob -> getResult();
}
$time_end_instance = microtime(true);
$time_instance = $time_end_instance - $time_start_instance ;
echo "\nTotal execution time for instance method is: '$time_instance' ";
?>
Output: Current PHP version: 7.0.32-0ubuntu0.16.04.1
Total execution time is for static method is: '0.0056149959564209'
Total execution time for instance method is: '0.004885196685791' |