PHP - Get Public Methods :: ReflectionClass???

tdktank59

Gawd
Joined
Jan 23, 2007
Messages
590
So im writing this permissions library that is based on class=>method restriction
aka: artificial crud, if you make each function have a specific function

Heres what I have right now.
It works however I have to manually tell it was to ignore. in other words it takes everything that I don't flag.

Code:
/**
	 * Auto Generate Permissions
	 * Generates all the systems permissions by reading the controller directory
	 * and loading the files to get their methods
	 */
	function auto_generate_permission()
	{
		// Open the Application Directory
		$directory = opendir($this->APPLICATION_DIRECTORY);

		// loop through all the files and set the name into an array
		while (($file = readdir($directory)) !== FALSE)
		{
			if (preg_match('/.php/i',$file))
			{
				$temp = explode('.',$file);
				$controllers[] = $temp[0];
			}
		}

		// Close the Application Directory
		closedir($directory);

		// Look through all the controllers for their methods
		foreach ($controllers as $c)
		{
			// Check to see if the class has been loaded
			if (!class_exists($c))
			{
				$this->CI->load->file($this->APPLICATION_DIRECTORY.$c.'.php');
			}

			// Add the constructors to the values we don't care about
			$this->invalid_methods[] = ucfirst($c);

			foreach (get_class_methods($c) as $key => $val)
			{
			    // Put the methods we care about into an array
				if (!in_array($val,$this->invalid_methods))
				{
						$method[$c][] = $val;
				}
			}

			// Now lets create the classes and methods and relate them
			foreach ($method as $class => $methods)
			{
				foreach ($methods as $value)
				{
					$this->permission($class,$value,FALSE);
				}
			}
		}
	}

I want to incorporate the ReflectionClass from PHP however it has very little documentation (more like none) So I have no idea where to begin.
I am not set on the reflection class if there is a better/easier way to do this.
Basically what I want to do is load a class and grab all public methods.
and to ignore the rest of them.

Any help is appreciated.
 
You could treat the files as text and do a look ahead regex for 'public' then a function_exists to make sure it's not a property. There are still quite a few problems with doing this and I am too tired to list them.

Reflection has ::getMethods and ::isPrivate. You can accomplish everything you want with those, but yes the documentation is very sparse. I haven't personally used them yet so I don't know much about their usage.
 
Well I only need to scan the files once in a blue moon (when ever I add more public functions to the application basically)

Ill see if I can somehow get the reflection stuff working... (They really need to document their functions...)
 
Thanks brandonjackson
He IM'd me this just a few mins ago.
I have yet to test it but looks promising.

Code:
foreach (get_class_methods($c) as $key => $val)
{
	/* Get a reflection object for the class method */
	$reflect = new ReflectionMethod($c, $val);
	
	/* For private, use isPrivate().  For protected, use isProtected() */
	
	if($reflect->isPublic())
	{
		if (!in_array($val,$this->invalid_methods))
		{
			// Put the methods we care about into an array
			$method[$c][] = $val;
		}
	}
}
 
I think this should solve your problem...

PHP:
			foreach (get_class_methods($c) as $key => $val)
			{
				  /* Get a reflection object for the class method */
       				$reflect = new ReflectionMethod($c, $val);

				/* For private, use isPrivate().  For protected, use isProtected() */
				
				if($reflect->isPublic()) 
				{
				  	if (!in_array($val,$this->invalid_methods))
					{
						// Put the methods we care about into an array
						$method[$c][] = $val;
					}
				}
			}
 
Back
Top