.:] $this->php(beans) [:.

August 8th, 2007 – 13:40:20

When I am working in Java, I code in IntelliJ which has this nice little macro to create all my getters and setters for the Bean object that I am working on at that point in time. It is a huge time saver. I recently started a new project in PHP and found myself desiring the same functionality in my PHP IDE, which didn`t exist. After a little bit of research, I decided I could do one of two things, either hack together a little macro to do it for me, or just create some function that would do it dynamically, on the fly… Here is what I came up with, it is likely that this will end up as part of a bigger PHP Framework that I will work on when I have some spare time (notice how long it has been between blog entries to get a clue how much spare time I have had lately) that will be some port of the JavaBeans API for PHP… Anyhow, I hope you enjoy!

/**
 * Base PHPBeanFramework Object to handle Dynamic Bean Functions
 *
 * This class will handle dynamically "creating" your getters/setters for
 * Bean Properties, re-instantiating your ReflectionObject when an implementing
 * class is deserialized from Session or Stream (DB/File) and will also create
 * your dynamic __toString() implementation to dump the values of the
 * implementation object.
 *
 *
 * Changelog:
 * 		1.0		7/28/07		Initial Creation
 * 		1.1		7/30/07		Changed __toString() to use a local array for
 * 							$class->getProperties since it was blowing up
 * 							due to trying to reference a temporary array.
 *
 * @author 		Chris Schmidt 
 * @package 	PHPBeanFramework/core
 * @version 	1.1
 * @since 		1.0.0
 * @copyright 	Copyright © 2007 - Digital-Ritual LLC.
 * @todo 		Make __call smart enough to work with boolean values as well.
 * @todo 		Allow a decorator to be injected for __toString()
 */
abstract class AbstractDataObject extends Object {
	/**
	 * A instance of ReflectionClass to represent the implementation object
	 * for dynamic methods defined in this base class.
	 *
	 * @var ReflectionClass
	 */
	private $clazz;	

	public function __construct() {
		$this->clazz = new ReflectionClass($this);
	}

	/**
	 * Generates, on the fly, a dynamic getter/setter pair for public properties
	 * of the implementation class. Note that case-sensitivity is in force here
	 * so if you have the following implementation class
	 *
	 * class X extends AbstractDataObject {
	 * 		public $vAr;
	 *
	 * 		public function __construct() {
	 * 			parent::__construct();
	 * 			$this->$vAr = "YO!";
	 * 		}
	 * }
	 *
	 * $x = new X();
	 * print($x->getvAr());		// Prints YO!
	 * print($x->getVaR());		// Fails
	 *
	 * @param string $method
	 * @param array $arguments
	 * @return mixed
	 */
	public function __call( $method, $arguments ) {
		if ( String::startsWith($method, "get") || String::startsWith($method, "set") ) {
			$property = strtolower(substr($method,3, 1)).substr($method,4);
			if ( $this->clazz->hasProperty( $property ) && $this->clazz->getProperty($property)->isPublic() ) {
				if ( String::startsWith( $method, "get" )) {
					return $this->clazz->getProperty( $property )->getValue( $this );
				} else {
					$this->clazz->getProperty( $property )->setValue( $this, $arguments[0] );
					return true;
				}
			}
		}

		error_log( "No Such Method: ".get_class($this)."::".$method);
		return false;
	}

	/**
	 * Dumps out the properties and values for the implementation class.
	 *
	 * @return string
	 */
	public function __toString() {
		$out = "";
		$props = $this->clazz->getProperties();
		foreach ( $props as &$prop ) {
			if ( $prop->isPublic() ) {
				$out .= sprintf( "[%-20s] %80s: %s
", array( gettype($prop->getValue($this)),$prop->getName(), $prop->getValue( $this ) ) );
			}
		}
		return $out;
	}

	/**
	 * Override the wakeup function since for some reason ReflectionObjects do
	 * not serialize/deserialize correctly. When the implementation object is
	 * deserialized, this function will recreate the ReflectionObject for this
	 * class.
	 */
	public function __wakeup() { $this->clazz = new ReflectionObject($this); }
}
?>

80 Comments

» Leave a comment now

» RSS feed for comments on this post
» TrackBack URI

  1. 1


    Pillspot.org. Canadian Health&Care.Special Internet Prices.Best quality drugs.No prescription online pharmacy. High quality pills. Order pills online

    Buy:Prevacid.Petcam (Metacam) Oral Suspension.Prednisolone.100% Pure Okinawan Coral Calcium.Actos.Zyban.Zovirax.Accutane.Nexium.Arimidex.Retin-A.Lumigan.Synthroid.Mega Hoodia.Human Growth Hormone.Valtrex….

    Trackback made by FREDRICK on 06.07.2010 @ 12:03:52

  2. 2


    MedicamentSpot.com. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Best quality drugs. No prescription drugs. Buy pills online

    Buy:Lumigan.Prevacid.Accutane.Zovirax.Arimidex.Mega Hoodia.Nexium.Zyban.Human Growth Hormone.Actos.100% Pure Okinawan Coral Calcium.Synthroid.Petcam (Metacam) Oral Suspension.Valtrex.Prednisolone.Retin-A….

    Trackback made by RONNIE on 21.07.2010 @ 22:27:47

  3. 3

    Brakes http://ul6.qiu.ii33.co : Are…

    What…

    Trackback made by What on 14.09.2010 @ 21:26:38

  4. 4

    abilify side effects in children

    Buydrugs without prescription…

    Trackback made by LONNIE on 20.12.2010 @ 10:35:14

  5. 5

    Advair@official.site” rel=”nofollow”>.

    Buynow it…

    Trackback made by EUGENE on 28.12.2010 @ 14:24:52

  6. 6

    Ampicillin@official.site” rel=”nofollow”>.

    Buynow it…

    Trackback made by KELLY on 30.12.2010 @ 08:35:53

  7. 7

    ..

    Buygeneric drugs…

    Trackback made by LARRY on 31.12.2010 @ 00:22:06

  8. 8

    Augmentin@official.site” rel=”nofollow”>.

    Buywithout prescription…

    Trackback made by ALBERTO on 01.01.2011 @ 06:20:11

  9. 9

    Clonidine@official.site” rel=”nofollow”>…

    Buynow…

    Trackback made by RICK on 01.01.2011 @ 14:55:09

  10. 10

    celexa@canada.now” rel=”nofollow”>.

    Buynow…

    Trackback made by RICK on 02.01.2011 @ 03:01:43

  11. 11

    Abilify@official.site” rel=”nofollow”>…

    Buynow…

    Trackback made by DOUGLAS on 17.01.2011 @ 03:22:21

  12. 12

    buy@generic.LEVITRA” rel=”nofollow”>…

    Need cheap generic LEVITRA?…

    Trackback made by Alexander7 on 22.07.2011 @ 19:11:30

  13. 13

    Purchase@Discount.Coral.Calcium” rel=”nofollow”>..

    Buyit now…

    Trackback made by TERRENCE on 29.07.2011 @ 02:19:14

  14. 14

    Cheap@Coral.Calcium.Online” rel=”nofollow”>..

    Buygeneric meds…

    Trackback made by SALVADOR on 29.07.2011 @ 02:44:08

  15. 15

    Buy@Discount.Abana” rel=”nofollow”>..

    Buyno prescription…

    Trackback made by BRANDON on 29.07.2011 @ 04:24:13

  16. 16

    Order@Acai.Without.Prescription” rel=”nofollow”>…

    Buynow…

    Trackback made by SALVADOR on 31.07.2011 @ 02:22:25

  17. 17

    Purchase@Abilify.Without.Prescription” rel=”nofollow”>..

    Buygeneric pills…

    Trackback made by EDUARDO on 01.08.2011 @ 15:05:14

  18. 18

    Buy@Coral.Calcium.Online” rel=”nofollow”>……

    Buywithout prescription…

    Trackback made by PERRY on 01.08.2011 @ 23:18:26

  19. 19

    Buy@Cheap.Coral.Calcium” rel=”nofollow”>.

    Buydrugs without prescription…

    Trackback made by MIKE on 01.08.2011 @ 23:52:08

  20. 20

    Buy@Abana.Online” rel=”nofollow”>.

    Buynow it…

    Trackback made by EDUARDO on 02.08.2011 @ 06:12:53

  21. 21

    Buy@Acai.Online” rel=”nofollow”>…

    Buyno prescription…

    Trackback made by LANCE on 03.08.2011 @ 13:27:15

  22. 22

    Buy@Acai.Online” rel=”nofollow”>.

    Buyit now…

    Trackback made by DENNIS on 03.08.2011 @ 13:27:15

  23. 23

    Buy@Discount.Acai” rel=”nofollow”>…

    Buyit now…

    Trackback made by FELIX on 03.08.2011 @ 16:02:40

  24. 24

    Order@Acai.Without.Prescription” rel=”nofollow”>.

    Buygeneric drugs…

    Trackback made by VICTOR on 04.08.2011 @ 17:09:50

  25. 25

    Cheap@Acai.500mg” rel=”nofollow”>..…

    Buygeneric drugs…

    Trackback made by DUSTIN on 04.08.2011 @ 20:38:03

  26. 26

    Order@Energy.Boost.Online” rel=”nofollow”>.…

    Buygeneric drugs…

    Trackback made by RICK on 05.08.2011 @ 02:05:57

  27. 27

    Purchase@Cheap.Energy.Boost” rel=”nofollow”>.…

    Buyno prescription…

    Trackback made by MATHEW on 05.08.2011 @ 06:37:05

  28. 28

    Buy@Accutane.10mg.20mg” rel=”nofollow”>.…

    Buygeneric drugs…

    Trackback made by BILLY on 06.08.2011 @ 21:59:09

  29. 29

    Buy@Aciphex.Online” rel=”nofollow”>.…

    Buygeneric pills…

    Trackback made by WILLIAM on 07.08.2011 @ 03:29:48

  30. 30

    Buy@Generic.Aciphex.Without.Prescription” rel=”nofollow”>.…

    Buyno prescription…

    Trackback made by ADAM on 07.08.2011 @ 21:29:01

  31. 31

    Buy@Actonel.Without.Prescription” rel=”nofollow”>.…

    Buygeneric meds…

    Trackback made by ARTHUR on 10.08.2011 @ 00:30:13

  32. 32

    Purchase@Generic.Actonel.35mg” rel=”nofollow”>..

    Buygeneric drugs krg…

    Trackback made by DARYL on 10.08.2011 @ 18:04:22

  33. 33

    Order@Actos.Online” rel=”nofollow”>..

    Buygeneric pills zdg…

    Trackback made by VICTOR on 11.08.2011 @ 04:04:27

  34. 34

    Purchase@Generic.Actos” rel=”nofollow”>.

    Buygeneric drugs zfk…

    Trackback made by RAY on 11.08.2011 @ 14:19:03

  35. 35

    Order@Acular.Online” rel=”nofollow”>..

    Buygeneric meds qmo…

    Trackback made by CLIFTON on 12.08.2011 @ 04:02:12

  36. 36

    Buy@Advair.Online” rel=”nofollow”>..

    Buynow it vhx…

    Trackback made by MARCUS on 12.08.2011 @ 15:53:22

  37. 37

    Buy@Acai.Without.Prescription” rel=”nofollow”>.

    Buygeneric drugs itj…

    Trackback made by RONALD on 14.08.2011 @ 12:49:58

  38. 38

    Buy@Energy.Boost.Online” rel=”nofollow”>..

    Buynow it cey…

    Trackback made by CHRISTOPHER on 15.08.2011 @ 06:25:26

  39. 39

    Purchase@Energy.Boost.Online” rel=”nofollow”>..

    Buydrugs without prescription faj…

    Trackback made by MITCHELL on 15.08.2011 @ 13:11:09

  40. 40

    accupril@and.muscle.pain” rel=”nofollow”>…

    Buynow it zdg…

    Trackback made by BRUCE on 15.08.2011 @ 18:44:52

  41. 41

    Get@Accupril.Online” rel=”nofollow”>..

    Buyit now irl…

    Trackback made by GERARD on 16.08.2011 @ 16:26:59

  42. 42

    Buy@Cheap.Aciphex” rel=”nofollow”>…

    Buywithout prescription ccw…

    Trackback made by JASON on 18.08.2011 @ 00:33:02

  43. 43

    Cheap@Aciphex.Online” rel=”nofollow”>..

    Buyno prescription gdt…

    Trackback made by MARION on 18.08.2011 @ 10:07:04

  44. 44

    Buy@Generic.Aciphex.Without.Prescription” rel=”nofollow”>…

    Buyit now knc…

    Trackback made by DALE on 18.08.2011 @ 16:00:22

  45. 45

    Buy@Generic.Actonel.Without.Prescription” rel=”nofollow”>.

    Buygeneric drugs ouw…

    Trackback made by DWIGHT on 22.08.2011 @ 02:11:05

  46. 46

    Buy@Cheap.Advair” rel=”nofollow”>.

    Buygeneric drugs hwi…

    Trackback made by RICARDO on 22.08.2011 @ 07:35:24

  47. 47

    Purchase@Advair.Online” rel=”nofollow”>..

    Buyno prescription jui…

    Trackback made by FRED on 23.08.2011 @ 03:48:17

  48. 48

    Purchase@Generic.Advair” rel=”nofollow”>.

    Buynow it lmg…

    Trackback made by VINCENT on 23.08.2011 @ 19:34:30

  49. 49

    Buy@Generic.Advair.25mcg50mcg.25mcg125mcg.25mcg250mcg.50mcg500mcg.50mcg100mcg.50mcg250mcg” rel=”nofollow”>..< …

    Buydrugs without prescription hak…

    Trackback made by MILTON on 24.08.2011 @ 05:02:58

  50. 50

    Trackback made by NICK on 24.08.2011 @ 06:25:01

  51. 51

    Order@Aggrenox.Online” rel=”nofollow”>…

    Buyit now sge…

    Trackback made by ALEXANDER on 24.08.2011 @ 18:58:46

  52. 52

    aciphex@cancer.buy” rel=”nofollow”>..

    Buygeneric drugs…

    Trackback made by ANGELO on 26.08.2011 @ 19:56:31

  53. 53

    can@you.get.acai.berries.in.the.grocery.store” rel=”nofollow”>.

    Buydrugs without prescription…

    Trackback made by BRYAN on 28.08.2011 @ 03:47:16

  54. 54

    buy@real.strong.armour” rel=”nofollow”>.

    Buydrugs without prescription…

    Trackback made by TYLER on 29.08.2011 @ 10:20:08

  55. 55

    ….

    ..

    Trackback made by .. on 25.09.2011 @ 07:55:04

  56. 56

    mike divorce cancer tiffany

    Buy_generic pills…

    Trackback made by ALFREDO on 17.10.2011 @ 21:08:12

  57. 57

    boulder valley asthma and allergy clinic

    Buy_no prescription…

    Trackback made by PETER on 19.10.2011 @ 02:47:49

  58. 58

    red wine and lipitor

    Buy_no prescription…

    Trackback made by JEFFERY on 25.10.2011 @ 00:40:00

  59. 59

    follistim increase estradiol levels

    Buy_generic drugs…

    Trackback made by MILTON on 26.10.2011 @ 01:43:06

  60. 60

    effexor xr 75

    Buy_generic pills…

    Trackback made by JAY on 27.10.2011 @ 07:38:26

  61. 61

    genital erection

    Buy_now it…

    Trackback made by ADAM on 27.10.2011 @ 14:18:29

  62. 62

    low fat diets history

    Buy_drugs without prescription…

    Trackback made by DENNIS on 27.10.2011 @ 15:53:36

  63. 63

    hives rash from coming off prednisone

    Buy_generic drugs…

    Trackback made by JULIO on 29.10.2011 @ 07:11:25

  64. 64

    Trackback made by BILL on 01.11.2011 @ 02:39:23

  65. 65

    zocor and ms

    Buy_it now…

    Trackback made by STEVE on 01.11.2011 @ 10:57:09

  66. 66

    adderall then tylenol pm

    Buy_generic drugs…

    Trackback made by CASEY on 01.11.2011 @ 17:33:48

  67. 67

    abdominal pain lutenizing hormone

    Buy_generic drugs…

    Trackback made by NEIL on 01.11.2011 @ 22:38:14

  68. 68

    ic tramadol hcl 50 mg

    Buy_generic meds…

    Trackback made by SHAWN on 02.11.2011 @ 08:37:58

  69. 69

    Trackback made by KENT on 02.11.2011 @ 11:58:17

  70. 70

    hot dog hospital diet substitutions

    Buy_generic drugs…

    Trackback made by NATHAN on 03.11.2011 @ 23:47:02

  71. 71

    Trackback made by LEE on 07.11.2011 @ 05:28:42

  72. 72

    twin pregnancy obstetrician specialist dallas

    Buy_drugs without prescription…

    Trackback made by FREDDIE on 09.11.2011 @ 01:40:12

  73. 73

    Trackback made by PATRICK on 09.11.2011 @ 11:38:52

  74. 74

    Trackback made by ARTURO on 09.11.2011 @ 13:18:52

  75. 75

    menopause thyroid cancer

    Buy_without prescription…

    Trackback made by TERRENCE on 11.11.2011 @ 11:56:46

  76. 76

    Trackback made by IAN on 12.11.2011 @ 09:38:58

  77. 77

    free fast diet tips

    Buy_generic meds…

    Trackback made by REGINALD on 15.11.2011 @ 14:35:47

  78. 78

    buy Klonopin…

    .:] $this->php(beans) [:. « beef.b10g-2.0.0…

    Trackback made by buy Klonopin on 23.03.2013 @ 04:50:47

  79. 79

    Ativan…

    .:] $this->php(beans) [:. « beef.b10g-2.0.0…

    Trackback made by Ativan on 28.03.2013 @ 12:55:59

  80. 80

    Xanax…

    .:] $this->php(beans) [:. « beef.b10g-2.0.0…

    Trackback made by Xanax on 11.04.2013 @ 20:56:00


Leave a Comment

You must be logged in to post a comment.