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.
Related
This question already has answers here:
Can I create a PHP function that I can call without parentheses?
(6 answers)
Closed 6 years ago.
all php functions need () in the end. However, exit doesnt need that.
Can I create a function manually, which I can later execute without () ?
Even more, If I have full access to php installation?
p.s. please dont tell me answers "exit is not function" or etc (My question is not if "exit" is function or not). I want to know HOW TO ACHIEVE like that.
No you can't. You have to edit Base of PHP language to accomplish this.
exit , echo , print and etc are not function .
This question already has an answer here:
How can I access a property with an invalid name?
(1 answer)
Closed 6 years ago.
I have a question about attributes in PHP.
I have various Class with the same attributes, but with with different prefix.
Example:
$attr->a_field;
$attr2->b_field;
So, with another Class I want to access to them.
I tried:
$field = "{$prefix}_field";
$attr->{$field}
and it works perfect. But is any other way to doing this?
I tried also with:
$attr->{$prefix}_field;
$attr->{$prefix}{"_field"};
$attr->"{$prefix}_field";
etc and who I suppose I get PHP's errors
Thanks!
You can write it directly as $attr->{"{$prefix}_field"}, as shown in the docs.
You're looking into variable variables
$attr->{$prefix."_field"}
This question already has answers here:
Remove a child with a specific attribute, in SimpleXML for PHP
(18 answers)
Closed 7 years ago.
I know there are a lot of answers out there for this exact question, but none of them seem to help me solve my problem.
I have an xml file on my server, i need to use PHP SimpleXML to remove an element from the document. After some googling i found a number of answers saying to use unset() and then save the xml.
so i came up with this:
function deleteCourse($course){
$xml = self::getxml(); # get the XML file
unset($xml->xpath('course[name = "'.$course.'"]'));
$xml->asXml("data.xml");
}
now whenever i run this i get this error: PHP Fatal error: Can't use method return value in write context in blahblahLink on line 92
line 92 is unset($xml->xpath('course[name = "'.$course.'"]'));
I really hope somebody can help me out with this
unset won't work if you pass method return, pass variable content / array instead
This question already has answers here:
Does function definition order matter?
(7 answers)
Closed 1 year ago.
The following code runs in PHP
<?php
$foo = "Chocolate milkshake";
go($foo);
function go($param) {
echo $param;
}
?>
// Output: chocolate milkshake
See this Demo http://codepad.viper-7.com/ToApZa
This code runs without errors and prints specified output, why?
I thought this "function hoisting" only occurred in JavaScript
It doesn't matter where you declare your functions in PHP in most cases, as you've just proved :)
Take a look at this page for more details. The key point:
Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.
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!