473,466 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I get the name of the method I am in.

Purple
404 Recognized Expert Contributor
Hi all,

So get_class($this) will return my class name, how do I get the name of the method ?

Thanks Purple..
Aug 30 '07 #1
18 1300
ak1dnar
1,584 Recognized Expert Top Contributor
Try this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class _Class{
  3.   function myfunc1(){
  4.         return(true);
  5.     }
  6.   function myfunc2(){
  7.         return(true);
  8.     }
  9.  
  10. }
  11. $methods = get_class_methods(new _Class());
  12. print_r ($methods);
  13. //or
  14. print "<br>";
  15. print $methods[0];
  16. print "<br>";
  17. print $methods[1];
  18. ?> 
EDIT: Now Only I read the things on title :D
Is this solved your problem?
Aug 30 '07 #2
Purple
404 Recognized Expert Contributor
Thanks for the response ajaxrand but it didn't..

Using your code:

[PHP]<?php
class _Class{
function myfunc1(){
do_function_that_errors()
or die("error in class ". get_class(). " method ". Method name here() );
return(true);
}
function myfunc2(){
return(true);
}

}
$methods = get_class_methods(new _Class());
print_r ($methods);
//or
print "<br>";
print $methods[0];
print "<br>";
print $methods[1];
?>[/PHP]

Regards Purple
Aug 30 '07 #3
ak1dnar
1,584 Recognized Expert Top Contributor
Hey Purple, What you are trying to do?
Aug 30 '07 #4
Purple
404 Recognized Expert Contributor
Hi Ajaxrand,

I am reworking the diagnostics and logging component of a site before putting a whole new chunk of functionality live.. Cause its a live site I want to be able to trap and kill any bugs as fast as possible - to that end, the logging function currently catches some basic info about who, when, which machine and maybe what the user was doing when it all went wrong.

So I am looking at other ways to capture more specific info on where the code is upto at exception points, hence why I would like to know if there is an function which will return the method / function within a class that is being executed.

Hope that helps

Regards Purple
Aug 30 '07 #5
pbmods
5,821 Recognized Expert Expert
Heya, Purple.

Try using the __METHOD__ magic constant.

http://php.net/manual/en/language.co...predefined.php
Aug 30 '07 #6
Purple
404 Recognized Expert Contributor
Thanks pbmods,

thats the answer I was looking for, I think I need to spend time practising to be a magician - those magic methods and constants get me everytime :)

Reviewing the other constants, thats really cool, will help loads in trapping exceptions - I think you should write an article on this stuff...

Purple
Aug 30 '07 #7
pbmods
5,821 Recognized Expert Expert
Heya, Purple.

Noted.

All I have to do first is finish my current articles on PHPDocumenter, Mass Virtual Hosting on Localhost and print_r() for JavaScript.
Aug 30 '07 #8
ak1dnar
1,584 Recognized Expert Top Contributor
Yeah That's really cool pb!

Hey Purple, make sure to asked the questions with sufficient info! (just kidding)

Any probs here?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class userLogin
  3. {
  4.     function userLogin()
  5.     {
  6.         $this->dbName = 'purple';
  7.     }
  8.     function matchThem($thisName)
  9.     {
  10.         if(!($thisName == $this->dbName)) die('ERROR IN:'.__METHOD__);
  11.         echo 'Matching Record Found';
  12.     }
  13. }
  14. $newUser = new userLogin;
  15. $newUser->matchThem('purple1');
  16. ?> 
Aug 30 '07 #9
Purple
404 Recognized Expert Contributor
pbmods,

whatcha waiting for :)

Purple.
Aug 30 '07 #10
ak1dnar
1,584 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. do_function_that_errors() or die(WHAT EVER);
guys, Is this possible?
Aug 30 '07 #11
Purple
404 Recognized Expert Contributor
Hi Ajaxrand,

this is a snip of what I have

[PHP]class my_class{

class variables, contructor and some other functions
//*
//* exec_sql - execute a sql statement
//*
function exec_sql() {
if ($this->sql == null) die($this->error_logger(__method__,__line__));
$result_obj = odbc_exec($this->sql_conn,$this->sql) or die($this->error_logger(__method__,__line__));
return $result_obj;
}
//*
//* error loggging method
//*
function error_logger($method, $line) {
echo "errror in call from ".$method." on line ".$line;
// will write to diagnostic area and log
}

}[/PHP]

about the same in code terms...

Purple
Aug 30 '07 #12
pbmods
5,821 Recognized Expert Expert
Heya, Ajaxrand.

The reason why that works with mysql_connect(), et al is because those functions return false when they fail to connect.

You'll want to do this instead (PHP 5):
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.     do_function_that_errors();
  4. }
  5. catch( Exception $e )
  6. {
  7.     $msg = $e->getMessage();
  8.  
  9.     if( empty($msg) )
  10.     {
  11.         exit('P0wN3d!');
  12.     }
  13.     exit($msg);
  14. }
  15.  
PS. The Javascript article is almost finished; it just needs some more testers!
http://www.thescripts.com/forum/thread668433.html
Aug 30 '07 #13
Purple
404 Recognized Expert Contributor
Hi all,

Just reading debug_backtrace() function, will give me even more info on error - anyone used it ? comments, thoughts ?

Purple
Aug 30 '07 #14
Purple
404 Recognized Expert Contributor
I have just tried the debug_backtrace() function and I have found a new best friend - debugging from hell to heaven..

Now why didn't one of you point it out to me before !! All that wasted time..

If you haven't tried it give it a go - fantastic..

Just need to rewrite my error logger (again)

Purple
Aug 30 '07 #15
ak1dnar
1,584 Recognized Expert Top Contributor
Heya, Ajaxrand.
exit('P0wN3d!');
Thanks but this ! .
Aug 30 '07 #16
Purple
404 Recognized Expert Contributor
LOL - I am learning so much today

even found an urban dictionary..

Purple
Aug 30 '07 #17
pbmods
5,821 Recognized Expert Expert
Heya, Purple.

Thanks for mentioning debug_backtrace(). I don't know how I missed it.
Aug 30 '07 #18
Atli
5,058 Recognized Expert Expert
While were talking about backtrace...

If your using exceptions you can do:
Expand|Select|Wrap|Line Numbers
  1. try{
  2.   // Do stuff
  3. }
  4. catch(Exception $ex) {
  5.   echo "<pre>". $ex->getTraceAsString() ."</pre>";
  6. }
  7.  
Very helpful when debugging.
Aug 31 '07 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: CJ | last post by:
Why won't this work? I am passing the name of the form (I have two that use this validation script) but I keep getting an error. Error reads: "document.which_form.name is null or not an object" ...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
1
by: William H. Burling | last post by:
i have a nest of vb.vanilla collections. CollectionofHotelData.item("great Pequot").item("2000").item(22) The above works. I wrote the code that constructs the nested collections and i can...
7
by: clr | last post by:
I like to stamp trace logs with the name of the executing Class and Method. I can get the Class Name using GetType.Name and I can get a list of every Method in the class using...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
8
by: AngryGerbil | last post by:
hey, How do I acquire MethodInfo WITHOUT hardcoding method name as a string?!??!The fact I have to use xxx.Gettype.GetMethod("MyMethod", xxx) is making me want to drive an ice pick into my eye! I...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
11
by: Alexander Walker | last post by:
Hello I would like to write a method that allows me to pass a reference to an instance of a class, the name of a property of that class and a value to set that property to, the method would then...
9
by: | last post by:
Is it possible to get the name of the method that the code is currently executing in so that I can pass it to another method? For example, in void method1() I will call void method5(). In method5...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.