SilabGarza - RS Private Server  
Home Forum Play Register Store Market Highscores Mark Forums Read
Go Back   SilabGarza - RS Private Server > SilGar Media > Computers > Programming
Reload this Page Exporting a class using PHP Reflection


Exporting a class using PHP Reflection
Old
  (#1 (permalink))
Evolution is Offline
Also known as Anthony`
image
 
Evolution's Avatar
 
Posts: 43
Cookies Given: 0
Cookies Received: 0
Time Online: 2 h 50 m
   
Lightbulb Exporting a class using PHP Reflection - 02-25-2011, 11:06 PM

Just thought I would share some utility methods with getting information on specified classes. It scans a specified directory for php classes, stores them in an array, and is at your disposal. The main point of this is my emulation of PHP's export() function (mine is the exportClass() function).

Use as,
PHP Code:
require('ScanClasses.php');
$classes ScanClasses::getSingleton('Path to PHP files containing classes');
echo 
'<pre>';
$classes->exportClass('Test');
echo 
'</pre>'
ScanClasses.php
PHP Code:
<?php
class ScanClasses {
 private static 
$class;
 private 
$instance;
 private function 
__construct($path) {
  if (!
is_dir($path)) {
   die(
"Given path is not a valid directory!");
  }
 
  
$dir opendir($path);
  while (
false !== ($file readdir($dir))) {
   if (
stripos($file'.php') === false)
    continue;
   require(
$path $file);
       
self::$class[] = new ReflectionClass(str_replace(".php"""$file));
  }
 }
 
 public static function 
getSingleton($path) {
  if (
$instance != null)
   return 
$instance;
  return 
$instance = new ScanClasses($path);
 }
 
 
// Testing purposes - PHP's export()
 
public static function ex() {
  echo 
ReflectionClass::export(self::$class[0]);
 }
 
 public function 
getClasses() {
  
$classes = array();
  for (
$i 0$i count(self::$class); $i++)
   
array_push($classesself::$class[$i]->newInstanceArgs());
  return 
$classes;
 }
 
 public function 
getClass($class_name) {
  for (
$i 0$i count(self::$class); $i++)
   if (
self::$class[$i]->getName() == $class_name)
    return 
self::$class[$i]->newInstanceArgs();
  return 
null;
 }
 
 public function 
exportClass($clazz) {
  
// $parsed_class = array(); <- TODO
  
$class = new ReflectionClass($clazz);
  
$export "Class [ " . ($class->isInterface() ? 'interface' : ($class->isAbstract() ? 'abstract ' '') . 'class' ) . " " $class->getName() . " "
  
$parent $class->getParentClass();
  if (
$parent != null) {
   
$export .= 'extends ' $parent->getName() . ' ';
  }
  
$interfaces $class->getInterfaceNames();
  
$interface_count count($interfaces);
  if (
$interface_count 0) {
   
$export .= 'implements ';
   for (
$i 0$i $interface_count$i++)
    
$export .= $interfaces[$i] . ($i <= count($interface_count) ? '' ', ');
  }
  
$export .= " ] {" "\n";
 
  
$static_fields $class->getProperties(ReflectionMethod::IS_STATIC);
  
$static_field_count count($static_fields);
  
$export .= "\t- Static properties [" $static_field_count "] {";
  if (
$static_field_count 0) {
   for (
$i 0$i $static_field_count$i++) {
    
$field_property $static_fields[$i];
    
$export .= "\n\t\tStatic property [ " self::modifierIntToString($field_property->getModifiers()) . " $" $field_property->getName() . " ]";
   }
  }
  
$export .= "\n\t}\n";
  
$static_methods $class->getMethods(ReflectionMethod::IS_STATIC);
  
$static_method_count count($static_methods);
  
$export .= "\t- Static methods [" $static_method_count "] {";
  if (
$static_method_count 0) {
   for (
$i 0$i $static_method_count$i++) {
    
$method_property $static_methods[$i];
    
$export .= "\n\t\tStatic method [ " self::modifierIntToString($method_property->getModifiers()) . " " $method_property->getName() . " ]";
   }
  }
  
$export .= "\n\t}\n";
 
  
$fields $class->getProperties(ReflectionMethod::IS_ABSTRACT ReflectionMethod::IS_FINAL ReflectionMethod::IS_PROTECTED ReflectionMethod::IS_PUBLIC ReflectionMethod::IS_PRIVATE);
  
$field_count count($fields);
  if (
$field_count 0) {
   
$valid_fields 0;
   
$export_temp '';
   for (
$i 0$i $field_count$i++) {
    
$field_property $fields[$i];
    if (
$field_property->isStatic())
     continue;
    
$export_temp .= "\n\t\tProperty [ " self::modifierIntToString($field_property->getModifiers()) . " $" $field_property->getName() . " ]";
    
$valid_fields++;
   }
   
$export .= "\t- Properties [" $valid_fields "] {" $export_temp;
  } else
   
$export .= "\t- Properties [0] {";
  
$export .= "\n\t}\n";
 
  
$methods $class->getMethods(ReflectionMethod::IS_ABSTRACT ReflectionMethod::IS_FINAL ReflectionMethod::IS_PROTECTED ReflectionMethod::IS_PUBLIC ReflectionMethod::IS_PRIVATE);
  
$method_count count($methods);
  if (
$method_count 0) {
   
$export_temp '';
   
$valid_methods 0;
   for (
$i 0$i $method_count$i++) {
    
$method_property $methods[$i];
    if (
$method_property->isStatic())
     continue;
    
$export_temp .= "\n\t\tMethod [ " self::modifierIntToString($method_property->getModifiers()) . " " $method_property->getName() . " ]";
    
$valid_methods++;
   }
   
$export .= "\t- Methods [" $valid_methods "] {" $export_temp;
  } else
   
$export .= "\t- Methods [0] {";
  
$export .= "\n\t}\n";
 
  echo 
$export "}";
 }
 
 private function 
modifierIntToString($modifier) {
  
$str '';
  switch(
$modifier) {
   case 
256:
   case 
257:
   case 
65792:
    
$str 'public';
    break;
   case 
512:
   case 
513:
   case 
66048:
    
$str 'protected';
    break;
   case 
1024:
   case 
1025:
   case 
66560:
    
$str 'private';
    break;
   case 
8448:
    
$str 'constructor';
    break;
   case 
65794:
    
$str 'abstract';
    break;
  }
 
  return 
$str;
 }
}
An example run (however on a page it is properly formatted),
Quote:
Class [ abstract class Test implements Test2 ] { - Static properties [0] { } - Static methods [1] { Static method [ private lol ] } - Properties [1] { Property [ protected $static ] } - Methods [4] { Method [ constructor __construct ] Method [ protected lol2 ] Method [ abstract lol3 ] Method [ public lol4 ] }}
  
Old
  (#2 (permalink))
Iron Doom is Offline
hu3hu3hu3hu3hu3
image
 
Iron Doom's Avatar
 
Posts: 3,456
Cookies Given: 82
Cookies Received: 25
Time Online: 20 d 11 h 6 m
Clan: Silabgarza's army
   
Default 02-26-2011, 12:49 AM

ehm
if I can ask
wtf is this about?
  
Old
  (#3 (permalink))
Sao Godo is Offline
<span style="color: lavender">Nomen est omen</span>
image
 
Sao Godo's Avatar
 
Posts: 4,574
Cookies Given: 15
Cookies Received: 50
Time Online: 40 d 23 h 34 m
Clan: Win
   
Default 02-26-2011, 07:42 AM

lovro, read this before, you will understand http://www.tuxradar.com/practicalphp/16/4/0


-------
interesting post :3



Trusted Helper


  
Old
  (#4 (permalink))
Evolution is Offline
Also known as Anthony`
image
 
Evolution's Avatar
 
Posts: 43
Cookies Given: 0
Cookies Received: 0
Time Online: 2 h 50 m
   
Default 02-26-2011, 08:37 AM

Yea, when I was first coming across PHP Reflection I needed something to do which would help me grasp it more so I decided to do this. The bored life...
  
Old
  (#5 (permalink))
Furydragz is Offline
image
 
Furydragz's Avatar
 
Posts: 1,097
Cookies Given: 0
Cookies Received: 0
Time Online: 5 d 20 h 25 m
   
Default 02-27-2011, 07:51 AM

Asert is fail troll.

Last edited by Furydragz; 03-01-2011 at 01:10 PM.
  
Old
  (#6 (permalink))
Asert One is Offline
Senior Member
image
 
Asert One's Avatar
 
Posts: 498
Cookies Given: 0
Cookies Received: 0
Time Online: 5 d 23 h 6 m
   
Default 02-28-2011, 08:38 AM

Quote:
Originally Posted by Furydragz View Post
Shit, I needed this.
Thanks
bullshit, fuck off
  
Old
  (#7 (permalink))
Furydragz is Offline
image
 
Furydragz's Avatar
 
Posts: 1,097
Cookies Given: 0
Cookies Received: 0
Time Online: 5 d 20 h 25 m
   
Default 03-01-2011, 01:09 PM

Quote:
Originally Posted by Asert One View Post
bullshit, fuck off
LOOOOOOOOOOOOOOOOOOLLLLLLLLLLLLLLLLLLLLLl
IRLLLLLLLLLl
Fail troll.
Only REASON i DID THAT WAS TO GET PEOPLE LIKE YOU HERE.
  
Old
  (#8 (permalink))
Asert One is Offline
Senior Member
image
 
Asert One's Avatar
 
Posts: 498
Cookies Given: 0
Cookies Received: 0
Time Online: 5 d 23 h 6 m
   
Default 03-03-2011, 05:03 AM

Quote:
Originally Posted by Furydragz View Post


LOOOOOOOOOOOOOOOOOOLLLLLLLLLLLLLLLLLLLLLl
IRLLLLLLLLLl
Fail troll.
Only REASON i DID THAT WAS TO GET PEOPLE LIKE YOU HERE.
make me laugh more nigger
  
Old
  (#9 (permalink))
Brittany is Offline
SG
image
 
Brittany's Avatar
 
Posts: 24,709
Cookies Given: 611
Cookies Received: 3,789
Time Online: 203 d 20 h 29 m
   
Default 03-03-2011, 11:15 AM

Quote:
Originally Posted by Furydragz View Post


LOOOOOOOOOOOOOOOOOOLLLLLLLLLLLLLLLLLLLLLl
IRLLLLLLLLLl
Fail troll.
Only REASON i DID THAT WAS TO GET PEOPLE LIKE YOU HERE.
Only reason you did it, faggot, was to get another post on the forums.



Sometimes we just need a friend.
Someone to hold us when you can't stand.
Sometimes even to pretend like you matter for that split second that everything seems to be crashing down.
And sometimes it's just easier to say goodbye.

  
Old
  (#10 (permalink))
Furydragz is Offline
image
 
Furydragz's Avatar
 
Posts: 1,097
Cookies Given: 0
Cookies Received: 0
Time Online: 5 d 20 h 25 m
   
Default 03-05-2011, 06:06 AM

Quote:
Originally Posted by brittany View Post


only reason you did it, faggot, was to get another post on the forums.
k


?...
  
adsense code2.3
Closed Thread

Bookmarks

Exporting a class using PHP Reflection

Programming




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
adsense2.6
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 12:00 AM.





Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.