{"id":952,"date":"2017-03-20T10:45:22","date_gmt":"2017-03-20T05:15:22","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=952"},"modified":"2017-03-29T12:50:56","modified_gmt":"2017-03-29T07:20:56","slug":"difference-public-private-protected","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/difference-public-private-protected\/","title":{"rendered":"PHP-What is the difference between public, private, and protected"},"content":{"rendered":"<p>Available Visibility in PHP Classes<\/p>\n<p>There are 3 type of visibility available in php for controlling your property or method.<\/p>\n<p><span style=\"color: #000000;\"><strong>Public:<\/strong><\/span><\/p>\n<p>Public method or variable can be accessible from anywhere. I mean from inside the class, out side the class and in child class also.<\/p>\n<p><span style=\"color: #000000;\"><strong>Private: <\/strong><\/span><\/p>\n<p>Method or property with private visibility can only be accessible inside the class. You can not access private method or variable from outside of your class.<\/p>\n<p><span style=\"color: #000000;\"><strong>Protected: <\/strong><\/span><\/p>\n<p>Method or variable with protected visibility can only be access in the derived class. Or in other word in child class. Protected will be used in the process of inheritance.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-medium wp-image-960\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/2-300x102.png\" alt=\"\" width=\"300\" height=\"102\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/2-300x102.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/2.png 383w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><span style=\"color: #800000;\"><strong>Public:<\/strong><\/span><\/p>\n<p>Public visiblity is least restricted visibility available in php.<\/p>\n<p>If you will not define the visibity factor with your method or property then public will be by defautl applied.<\/p>\n<p>Public methods or variables can be accessible from anywhere<\/p>\n<p>.<strong>For example,<\/strong> \u00a0It can be accessible from using object(outside the class), or inside the class, or in child class.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php  Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">class test<br\/>{<br\/>public $abc;<br\/>public $xyz;<br\/>public function xyz()<br\/>{<br\/>}<br\/>}<br\/>$objA = new test();<br\/>echo $objA-&gt;abc;\/\/accessible from outside<br\/>$objA-&gt;xyz();\/\/public method of the class test<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><span style=\"color: #800080;\"><strong>Private:<\/strong><\/span><\/p>\n<p>Private method or properties can only be accessible within the class.<\/p>\n<p>You can not access private variable or function of the class by making object out side the class.<\/p>\n<p>But you can use private function and property within the class using $this object.<\/p>\n<p>Private\u00a0visibility\u00a0in php classes is used when you do not want your property or function to be exposed outside the class.<\/p>\n<p>example of Private visibility in php classes.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\"><br\/>Class test<br\/>{<br\/>public $abc;<br\/>private $xyz;<br\/>public function pubDo($a)<br\/>{<br\/>echo $a;<br\/>}<br\/>private function privDo($b)<br\/>{<br\/>echo $b;<br\/>}<br\/>public function pubPrivDo()<br\/>{<br\/>$this-&gt;xyz = 1;<br\/>$this-&gt;privDo(1);<br\/>}<br\/>}<br\/>$objT = new test();<br\/>$objT-&gt;abc = 3;\/\/Works fine<br\/>$objT-&gt;xyz = 1;\/\/Throw fatal error of visibility<br\/>$objT-&gt;pubDo(&quot;test&quot;);\/\/Print &quot;test&quot;<br\/>$objT-&gt;privDo(1);\/\/Fatal error of visibility<br\/>$objT-&gt;pubPrivDo();\/\/Within this method private function privDo and variable xyz is called using $this variable.<\/code><\/pre> <\/div>\n<p style=\"top: 671px;\">[ad type=&#8221;banner&#8221;]\n<p><span style=\"color: #ff6600;\"><strong>Protected:<\/strong><\/span><\/p>\n<p>Protected visibility in php\u00a0classes\u00a0are only useful in case of\u00a0inheritance\u00a0and interface.<\/p>\n<p>We will discuss in depth of interfaces and\u00a0inheritance\u00a0in other chapter of this tutorial.<\/p>\n<p>Protected method or variable can be accessible either within class or child class. Here we will take very basic example:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">class parent<br\/>{<br\/>protected $pr;<br\/>public $a<br\/>protected function testParent()<br\/>{<br\/>echo this is test;<br\/>}<br\/>}<br\/>class child extends parent<br\/>{<br\/>public function testChild()<br\/>{<br\/>$this-&gt;testParent(); \/\/will work because it<br\/>}<br\/>}<br\/>$objParent = new parent();<br\/>$objParent-&gt;testParent();\/\/Throw error<br\/>$objChild = new Child();<br\/>$objChild-&gt;setChild();\/\/work because test child will call test parent.<\/code><\/pre> <\/div>\n<p>If you will take analyze above section you can found that method test Parent() is not accessible from object of class. But it is accessible in child class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Available Visibility in PHP Classes There are 3 type of visibility available in php for controlling your property or method. Public: Public method or variable can be accessible from anywhere. I mean from inside the class, out side the class and in child class also. Private: Method or property with private visibility can only be [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[1795,1797,1796,1792,1799,1794,1798,1793],"class_list":["post-952","post","type-post","status-publish","format-standard","hentry","category-php","tag-php-default-visibility","tag-php-private-class","tag-php-protected-static","tag-php-protected-variable","tag-php-public-variable","tag-protected-function-c","tag-public-private-protected-c","tag-public-private-protected-in-php-examples"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=952"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/952\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}