{"id":4048,"date":"2022-01-25T14:56:32","date_gmt":"2022-01-25T14:56:32","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=4048"},"modified":"2022-02-17T07:13:46","modified_gmt":"2022-02-17T07:13:46","slug":"inheritance-of-a-class","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/classes\/inheritance-of-a-class","title":{"rendered":"Inheritance of a class"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Create a class that inherits from a parent class<\/h4>\n\n\n\n<p>Define a class that inherits from another<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyChildClass extends MyParentClass\n{\n  function MyChildFunction()\n  {\n    return \"abc\";\n  }\n}<\/code><\/pre>\n\n\n\n<p>Now, objects of class MyChildClass can call MyChildFunction(), but objects of MyParentClass can&#8217;t. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Overriding functions in a parent class<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyChildClass extends MyParentClass\n{\n\n  \/\/Override parent class function MyParentFunction()\n  function MyParentFunction()\n  {\n    return \"abc\";\n  }\n\n\n  \/\/If we wanted to call the function in the parent class\n  function SomeChildFunction()\n  {\n    $Something = parent::MyParentFunction();\n  }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Protected members<\/h4>\n\n\n\n<p>Members (variables, functions, etc) can be defined with one of the following protection levels:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public      \/\/(Default) Can be accessed inside and outside the class\nprotected   \/\/Can be accessed inside the class and any child class only (not outside the class)\nprivate     \/\/Can be accessed inside the class only (not by a child class or outside the class)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Create a class that inherits from a parent class Define a class that inherits from another Now, objects of class MyChildClass can call MyChildFunction(), but objects of MyParentClass can&#8217;t. Overriding functions in a parent class Protected members Members (variables, functions, etc) can be defined with one of the following protection levels:<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[315],"tags":[],"class_list":["post-4048","post","type-post","status-publish","format-standard","hentry","category-classes"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=4048"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4048\/revisions"}],"predecessor-version":[{"id":4057,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4048\/revisions\/4057"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=4048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=4048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=4048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}