{"id":15,"date":"2007-03-18T23:22:00","date_gmt":"2007-03-18T23:22:00","guid":{"rendered":"http:\/\/grummfy.be\/blog\/?p=15"},"modified":"2007-03-18T23:22:00","modified_gmt":"2007-03-18T23:22:00","slug":"jouons-aves-php-et-la-poo","status":"publish","type":"post","link":"https:\/\/grummfy.be\/blog\/15","title":{"rendered":"Jouons aves PHP et la poo"},"content":{"rendered":"<p>Amusons nous avec PHP4, et PHP5 en poo.<\/p>\n<p> Comparons le comportement de ceci :<\/p>\n<pre class=\"code php\"><span style=\"color: rgb(0, 0, 0); font-weight: bold;\">class<\/span> A<br \/><span style=\"color: rgb(102, 204, 102);\">{<\/span><br \/>    <span style=\"color: rgb(0, 0, 0); font-weight: bold;\">function<\/span> A<span style=\"color: rgb(102, 204, 102);\">(<\/span><span style=\"color: rgb(102, 204, 102);\">)<\/span><br \/>    <span style=\"color: rgb(102, 204, 102);\">{<\/span><\/pre>\n<p>\n<!--more--><\/p>\n<pre class=\"code php\">        <a href=\"http:\/\/www.php.net\/echo\"><span style=\"color: rgb(0, 0, 102);\">echo<\/span><\/a> <span style=\"color: rgb(255, 0, 0);\">'Je me nome A'<\/span> . <span style=\"color: rgb(255, 0, 0);\">\"<span style=\"color: rgb(0, 0, 153); font-weight: bold;\"> <\/span>\"<\/span>;<br \/>    <span style=\"color: rgb(102, 204, 102);\">}<\/span><br \/><br \/>    <span style=\"color: rgb(0, 0, 0); font-weight: bold;\">function<\/span> B<span style=\"color: rgb(102, 204, 102);\">(<\/span><span style=\"color: rgb(102, 204, 102);\">)<\/span><br \/>    <span style=\"color: rgb(102, 204, 102);\">{<\/span><br \/>        <a href=\"http:\/\/www.php.net\/echo\"><span style=\"color: rgb(0, 0, 102);\">echo<\/span><\/a> <span style=\"color: rgb(255, 0, 0);\">'Je me nome B'<\/span> . <span style=\"color: rgb(255, 0, 0);\">\"<span style=\"color: rgb(0, 0, 153); font-weight: bold;\"> <\/span>\"<\/span>;<br \/>    <span style=\"color: rgb(102, 204, 102);\">}<\/span><br \/><br \/><span style=\"color: rgb(102, 204, 102);\">}<\/span><br \/>&nbsp;<br \/><span style=\"color: rgb(0, 0, 0); font-weight: bold;\">class<\/span> B <span style=\"color: rgb(0, 0, 0); font-weight: bold;\">extends<\/span> A<br \/><span style=\"color: rgb(102, 204, 102);\">{<\/span><br \/>    <span style=\"color: rgb(0, 0, 0); font-weight: bold;\">function<\/span> C<span style=\"color: rgb(102, 204, 102);\">(<\/span><span style=\"color: rgb(102, 204, 102);\">)<\/span><br \/>    <span style=\"color: rgb(102, 204, 102);\">{<\/span><br \/>        <a href=\"http:\/\/www.php.net\/echo\"><span style=\"color: rgb(0, 0, 102);\">echo<\/span><\/a> <span style=\"color: rgb(255, 0, 0);\">'Je me nome C'<\/span> . <span style=\"color: rgb(255, 0, 0);\">\"<span style=\"color: rgb(0, 0, 153); font-weight: bold;\"> <\/span>\"<\/span>;<br \/>    <span style=\"color: rgb(102, 204, 102);\">}<\/span><br \/><span style=\"color: rgb(102, 204, 102);\">}<\/span><br \/><br \/><span style=\"color: rgb(0, 0, 0); font-weight: bold;\">class<\/span> C <span style=\"color: rgb(0, 0, 0); font-weight: bold;\">extends<\/span> B<br \/><span style=\"color: rgb(102, 204, 102);\">{<\/span><br \/>&nbsp;<br \/><span style=\"color: rgb(102, 204, 102);\">}<\/span><br \/>&nbsp;<br \/><span style=\"color: rgb(0, 0, 255);\">$B<\/span> = <span style=\"color: rgb(0, 0, 0); font-weight: bold;\">new<\/span> B<span style=\"color: rgb(102, 204, 102);\">(<\/span><span style=\"color: rgb(102, 204, 102);\">)<\/span>;<br \/><span style=\"color: rgb(0, 0, 255);\">$C<\/span> = <span style=\"color: rgb(0, 0, 0); font-weight: bold;\">new<\/span> C<span style=\"color: rgb(102, 204, 102);\">(<\/span><span style=\"color: rgb(102, 204, 102);\">)<\/span>;<\/pre>\n<ul>\n<li>en PHP4 :<br \/>\n<blockquote>\n<p>Je me nome B<br \/>\n Je me nome C<\/p>\n<\/blockquote>\n<\/li>\n<li>en PHP5<br \/>\n<blockquote>\n<p>Je me nome A <br \/>Je me nome A<\/p>\n<\/blockquote>\n<\/li>\n<\/ul>\n<p> <ins> Avant tout il faut savoi<\/ins>r que en PHP, si une class enfant n&rsquo;as pas de constructeur d\u00e9fini, c&rsquo;est le constructeur parent qui sera appel\u00e9.<\/p>\n<p> En PHP5, on peux le voir c&rsquo;est effectivement le cas.<br \/>Mais en PHP4, cela ne semble pas tout a fait juste.<\/p>\n<p>Changeons un peu le code, en rempla\u00e7ant :<br \/><code> function C()<\/code><br \/>par<br \/><code>function Cbis()<\/code> <br \/>Et l\u00e0 le r\u00e9sultat pour PHP4 change :<\/p>\n<blockquote>\n<p>Je me nome B<br \/>\n Je me nome B<\/p>\n<\/blockquote>\n<p>C&rsquo;est toujours utile a savoir&#8230; et surtotu cela peux \u00e9viter de longue heures de debug &#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Amusons nous avec PHP4, et PHP5 en poo.<\/p>\n<p> Comparons le comportement de ceci :<\/p>\n<pre class=\"code php\"><span style=\"color: rgb(0, 0, 0); font-weight: bold;\">class<\/span> A<br \/><span style=\"color: rgb(102, 204, 102);\">{<\/span><br \/>    <span style=\"color: rgb(0, 0, 0); font-weight: bold;\">function<\/span> A<span style=\"color: rgb(102, 204, 102);\">(<\/span><span style=\"color: rgb(102, 204, 102);\">)<\/span><br \/>    <span style=\"color: rgb(102, 204, 102);\">{<\/span><\/pre>\n<p><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"webmentions_disabled_pings":false,"webmentions_disabled":false,"footnotes":""},"categories":[10],"tags":[22,110,23,24],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-php","tag-jouons","tag-php","tag-poo","tag-programmation"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":0,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"wp:attachment":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}