php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #45309 Need the ability to check if a method is public, private, or protected
Submitted: 2008-06-19 03:40 UTC Modified: 2008-06-19 21:18 UTC
From: php at suteki dot org Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.6 OS: Linux (CentOS 5)
Private report: No CVE-ID: None
 [2008-06-19 03:40 UTC] php at suteki dot org
Description:
------------
I can't seem to be able to find any php functions that would check if a method is public, private or protected before calling it.  This would be used in a custom framework that loads classes as plugins.  Right now if a method is private and the framework trys to call it and has no access, I get a fatal error (which I would like to avoid).  I've worked around it for the time by doing some goofy naming in my class and putting some code into the function that dynamically calls the methods, but I think there should be a better way to handle things.

Reproduce code:
---------------
This is how I'm working around it right now...

// Load the requested class.  Then run the requested method.  If the method is not available, run the default event.
// Please name private methods with two leading underscores ( __ ) to prevent errors from occuring because PHP has no way of checking if an event is
// private before it trys to run it..
$currentModule = url::get()->moduleID;
$currentEvent = substr(url::get()->GET['event'], 0, 2) != '__' && url::get()->GET['event'] ? url::get()->GET['event'] : config::$defaultEvent;
$$currentModule = new $currentModule;
if (method_exists($$currentModule, $currentEvent)) {
        $$currentModule->$currentEvent();
}


Expected result:
----------------
I would like to do something like this if possible...

// Load the requested module.  Then run the requested event.  If the event is not available, run the default event.
$currentModule = url::get()->moduleID;
$currentEvent = url::get()->GET['event'] ? url::get()->GET['event'] : config::$defaultEvent;
$$currentModule = new $currentModule;
if (method_exists($$currentModule, $currentEvent) && method_encapsulation($$currentModule, $currentEvent) == 'public') {
        $$currentModule->$currentEvent();
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-19 09:16 UTC] felix dot devliegher at gmail dot com
Have a look at SPL Reflection (http://be.php.net/reflection).
All functionality for getting a method's visibility is in there.
 [2008-06-19 21:18 UTC] johannes@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

See previous comment.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Nov 21 21:00:01 2025 UTC