This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
get current class and method?
How can i find the name of the method i am using in php? I found how to do this in C but not in PHP. I found a Q on here which roughly talked about magic constants (here) but I didn't really get it. In the following example I want $thisMethodName to be 'model_databaseLogin'
EG:
public function model_databaseLogin()
{
$thisMethodName = ... ;
return $this->model_methodCheck( $thisMethodName );
}
Is this possible in php?
You need the "magic constant" __METHOD__. The magic constant docs should be helpful.
So your code would be:
public function model_databaseLogin() {
$thisMethodName = __METHOD__;
return $this->model_methodCheck($thisMethodName);
}
The simplest answer is the magic constants to which you refer; specifically __FUNCTION__
These are called "magic" because their value is actually contextually dynamic.
public function model_databaseLogin()
{
$thisMethodName = __FUNCTION__;
return $this->model_methodCheck( $thisMethodName );
}
There is another way, via debug_backtrace(), but that is decidedly less efficient!
Related
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 5 years ago.
class ClassOne
{
private $foo = null;
public function __construct()
{
$this->foo = new ClassTwo();
}
public function doStuff()
{
$bar = &$this->foo->someVariable;
}
}
I know that $this refers to the current object, and & is a reference symbol for changing the original variable.
What is &$this referring to?
Thanks to #k0pernikus and #trincot for clarifying that & refers to the entire $this->foo->someVariable
So the original author wanted to change someVariable in $this->foo->someVariable. Since it was verbose, he assigned the reference to $bar.
However, this was done in PHP4. There must be a better way to write this in PHP5.
This question already has answers here:
What does it mean to start a PHP function with an ampersand?
(3 answers)
Closed 7 years ago.
I'm using a CMS package written in PHP. In one of it's core files I saw following line that is for defining a function in a class body.
public static function &getLib($sClass, $aParams = array()) {
// Code
}
I didn't understand why the function name 'getLib' has been prepended with the ampersand(&) sign? I've never seen such thing before.
Can someone please explain me in detail why such thing has been done and what's the benefit it has over simply using the function name?
It means the function should return a reference to a variable rather than just the value itself.
This question already exists:
Closed 10 years ago.
Possible Duplicate:
setting scope of array_map php
I have a function called cube1() in a class called customExceptions.
In another PHP script I need to use array_map(), and for the callback function I want to use the cube1() function in the customExceptions class.
What is the syntax to do this? This seems a really basic question but I could't find a simple straight forward answer.
<?php
class customExceptions{
static public function cube1($i){
return $i*$i*$i;
}
}
$arr = array(1,2,3,4);
print_r($arr);
$arr2 = array_map(array('customExceptions', 'cube1'), $arr);
print_r($arr2);
?>
Shouldn't this work?
customExceptions::cube1(array_map());
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP class instantiation. To use or not to use the parenthesis?
Omission of brackets and parameter-less object constructors
With or without the brackets, the new Class seems not bother. So, I doubt what's the usage of the brackets (). I searched php manual, didn't get it. Could anybody explain?
The purpose of the brackets is for you to enter any arguments that your constructor may accept.
class Example{
private $str;
public function __construct($str){
$this->str = $str;
}
public function output(){
echo $this->str;
}
}
$ex = new Example; // missing argument error
$ex = new Example('Something');
$ex->output(); // echos "Something"
If your class constructor does not accept any arguments, you may leave the brackets out. For good code sake, I always keep the brackets, whether or not the constructor accepts any argument.
Most coders coming from C# or Java background would keep the parenthesis as it is more familar to them.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Find out which class called a method in another class.
Hello everyone,
I have a class and I can't find where his object creates.
I mean, can I somehow find out who calls his constructor?
Thanks a lot,
Andrey.
use
$trace = debug_backtrace();
echo "<pre>".print_r($trace[1])."</pre>";
//see all the displays '1' is the referrer '0' is self
echo $callingfunction = $trace[1]['function'];
echo $callingclass = $trace[1]['class'];
You can use debug_backtrace() or even better a tracer/profiler like Xdebug to gather the information and e.g. KCachegrind to visualize it.