{"id":273,"date":"2010-11-21T02:19:41","date_gmt":"2010-11-21T01:19:41","guid":{"rendered":"https:\/\/d-mueller.de\/blog\/?p=273"},"modified":"2010-11-26T23:31:03","modified_gmt":"2010-11-26T22:31:03","slug":"reflection-teil-4-reflectionproperty","status":"publish","type":"post","link":"https:\/\/d-mueller.de\/blog\/reflection-teil-4-reflectionproperty\/","title":{"rendered":"Reflection \u2013 Teil 4: ReflectionProperty"},"content":{"rendered":"<p>\n<b>Vorangegangene Teile:<\/b><\/p>\n<ul>\n<li><a href=\"https:\/\/d-mueller.de\/blog\/reflection-teil-1-reflectionclass\/\">Teil 1: Einf\u00fchrung und ReflectionClass<\/a><\/li>\n<li><a href=\"https:\/\/d-mueller.de\/blog\/reflection-teil-2-reflectionmethod-und-reflectionfunction\/\">Teil 2: ReflectionMethod und ReflectionFunction<\/a><\/li>\n<li><a href=\"https:\/\/d-mueller.de\/blog\/reflection-teil-3-reflectionparameter\/\">Teil 3: ReflectionParameter<\/a><\/li>\n<\/ul>\n<p>Nun sind also die Properties dran, womit dann der vierteilige Reflection-Ausflug auch schon beendet ist. Wie bekannt beginnen wir wieder mit einem Mustercode<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\nclass D\r\n{\r\n\t\/**\r\n\t * this is a property\r\n\t *\/\r\n\tpublic $prop = array(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;);\r\n}\r\n<\/pre>\n<h3>Property erhalten<\/h3>\n<p>Der Weg um an das ReflectionProperty &#8211; Objekt zu kommen f\u00fchrt uns \u00fcber das ReflectionClass &#8211; Objekt der Klasse.<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\n$refclass = new ReflectionClass(&quot;D&quot;);\r\n$refprop = $refclass-&gt;getProperty(&quot;prop&quot;);\r\n<\/pre>\n<h3>DocBlock &#8211; Kommentar<\/h3>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\nprint_r($refprop-&gt;getDocComment());\r\n\/**\r\n * this is a property\r\n *\/\r\n<\/pre>\n<h3>Property-Name<\/h3>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\nprint $refprop-&gt;getName(); \r\n\/\/prop\r\n<\/pre>\n<h3>Property-Value<\/h3>\n<p>Hierbei erwartet <i>getValue<\/i> eine Instanz der Klasse als Parameter. Falls es sich um ein statisches Property handelt, ist hier <i>null<\/i> zu verwenden.<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\nprint_r($refprop-&gt;getValue(new D)); \r\n\/*\r\nArray \r\n( \r\n    [0] =&gt; a \r\n\t[1] =&gt; b \r\n\t[2] =&gt; c \r\n)\r\n*\/\r\n<\/pre>\n<p>Handelt es sich nicht um ein Public oder ein Protected &#8211; Property, fliegt eine ReflectionException.<\/p>\n<h3>Handelt es sich um ein Default-Property?<\/h3>\n<p>Steht das Property direkt im Code (zur &#8222;Compilezeit&#8220; bekannt) oder kam es erst zur Laufzeit hinzu (<i>$this->newProperty = &#8230;<\/i>)?<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\nvar_dump($refprop-&gt;isDefault());\r\n\/\/bool(true)\r\n<\/pre>\n<h3>Modifier pr\u00fcfen<\/h3>\n<p>Weitere Funktionen, die ebenfalls einen booleschen Wert zur\u00fcckliefern sind hier: <i>isPrivate, isProtected, isStatic<\/i>.<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\nvar_dump($refprop-&gt;isPublic());\r\n\/\/bool(true)\r\n<\/pre>\n<h3>Das wars!<\/h3>\n<p>Es sei abschlie\u00dfend noch erw\u00e4hnt, dass alle oben gemachten Aussasgen auch f\u00fcr statische Properties g\u00fcltig sind. Wie in den anderen Teilen auch ist dies hier keine vollst\u00e4ndige Auflistung, daf\u00fcr schaut ihr am Besten direkt im <a href=\"http:\/\/php.net\/manual\/en\/class.reflectionproperty.php\">Handbuch<\/a> nach.<\/p>\n<p><b>Und damit wars das mit der Reihe \u00fcber die Reflection &#8211; hoffe ihr habt was mitgenommen!<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vorangegangene Teile: Teil 1: Einf\u00fchrung und ReflectionClass Teil 2: ReflectionMethod und ReflectionFunction Teil 3: ReflectionParameter Nun sind also die Properties dran, womit dann der vierteilige Reflection-Ausflug auch schon beendet ist. Wie bekannt beginnen wir wieder mit einem Mustercode class D &hellip; <a href=\"https:\/\/d-mueller.de\/blog\/reflection-teil-4-reflectionproperty\/\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-273","post","type-post","status-publish","format-standard","hentry","category-php","category-webdev"],"_links":{"self":[{"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/posts\/273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/comments?post=273"}],"version-history":[{"count":0,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/posts\/273\/revisions"}],"wp:attachment":[{"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/media?parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/categories?post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/tags?post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}