{"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[pastacode lang=\u201dphp\u201d manual=\u201dclass%20test%0A%7B%0Apublic%20%24abc%3B%0Apublic%20%24xyz%3B%0Apublic%20function%20xyz()%0A%7B%0A%7D%0A%7D%0A%24objA%20%3D%20new%20test()%3B%0Aecho%20%24objA-%3Eabc%3B%2F%2Faccessible%20from%20outside%0A%24objA-%3Exyz()%3B%2F%2Fpublic%20method%20of%20the%20class%20test%0A\u201d message=\u201dPhp  Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201dphp\u201d manual=\u201d%0AClass%20test%0A%7B%0Apublic%20%24abc%3B%0Aprivate%20%24xyz%3B%0Apublic%20function%20pubDo(%24a)%0A%7B%0Aecho%20%24a%3B%0A%7D%0Aprivate%20function%20privDo(%24b)%0A%7B%0Aecho%20%24b%3B%0A%7D%0Apublic%20function%20pubPrivDo()%0A%7B%0A%24this-%3Exyz%20%3D%201%3B%0A%24this-%3EprivDo(1)%3B%0A%7D%0A%7D%0A%24objT%20%3D%20new%20test()%3B%0A%24objT-%3Eabc%20%3D%203%3B%2F%2FWorks%20fine%0A%24objT-%3Exyz%20%3D%201%3B%2F%2FThrow%20fatal%20error%20of%20visibility%0A%24objT-%3EpubDo(%22test%22)%3B%2F%2FPrint%20%22test%22%0A%24objT-%3EprivDo(1)%3B%2F%2FFatal%20error%20of%20visibility%0A%24objT-%3EpubPrivDo()%3B%2F%2FWithin%20this%20method%20private%20function%20privDo%20and%20variable%20xyz%20is%20called%20using%20%24this%20variable.%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"top: 671px;\">[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201dphp\u201d manual=\u201dclass%20parent%0A%7B%0Aprotected%20%24pr%3B%0Apublic%20%24a%0Aprotected%20function%20testParent()%0A%7B%0Aecho%20this%20is%20test%3B%0A%7D%0A%7D%0Aclass%20child%20extends%20parent%0A%7B%0Apublic%20function%20testChild()%0A%7B%0A%24this-%3EtestParent()%3B%20%2F%2Fwill%20work%20because%20it%0A%7D%0A%7D%0A%24objParent%20%3D%20new%20parent()%3B%0A%24objParent-%3EtestParent()%3B%2F%2FThrow%20error%0A%24objChild%20%3D%20new%20Child()%3B%0A%24objChild-%3EsetChild()%3B%2F%2Fwork%20because%20test%20child%20will%20call%20test%20parent.%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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}]}}