fork download
  1. <?php
  2.  
  3. $data = <<<'DATA'
  4.  
  5. <?php
  6.  
  7. // base class with member properties and methods
  8. class Vegetable {
  9.  
  10. var $edible;
  11. var $color;
  12.  
  13. function Vegetable($edible, $color="green")
  14. {
  15. $this->edible = $edible;
  16. $this->color = $color;
  17. }
  18.  
  19. function is_edible()
  20. {
  21. return $this->edible;
  22. }
  23.  
  24. function what_color()
  25. {
  26. return $this->color;
  27. }
  28.  
  29. } // end of class Vegetable
  30.  
  31. // extends the base class
  32. class Spinach extends Vegetable {
  33.  
  34. var $cooked = false;
  35.  
  36. function Spinach()
  37. {
  38. parent::__construct(true, "green");
  39. }
  40.  
  41. function cook_it()
  42. {
  43. $this->cooked = true;
  44. }
  45.  
  46. function is_cooked()
  47. {
  48. return $this->cooked;
  49. }
  50.  
  51. } // end of class Spinach
  52.  
  53. ?>
  54. DATA;
  55.  
  56.  
  57. $class_regex = '~
  58. ^\s*class\s+
  59. (?P<class>\S+)[^{}]+(\{
  60. (?:[^{}]*|(?2))*
  61. \})~mx';
  62.  
  63. $data = preg_replace_callback($class_regex,
  64. function($match) {
  65. $function_regex = '~function\s+\K'.$match['class'].'~';
  66. return preg_replace($function_regex, '__construct', $match[0]);
  67. },
  68. $data);
  69.  
  70. echo $data;
  71. ?>
  72.  
Success #stdin #stdout 0.01s 23576KB
stdin
Standard input is empty
stdout
<?php

// base class with member properties and methods
class Vegetable {

   var $edible;
   var $color;

   function __construct($edible, $color="green")
   {
       $this->edible = $edible;
       $this->color = $color;
   }

   function is_edible()
   {
       return $this->edible;
   }

   function what_color()
   {
       return $this->color;
   }

} // end of class Vegetable

// extends the base class
class Spinach extends Vegetable {

   var $cooked = false;

   function __construct()
   {
       parent::__construct(true, "green");
   }

   function cook_it()
   {
       $this->cooked = true;
   }

   function is_cooked()
   {
       return $this->cooked;
   }

} // end of class Spinach

?>