{"id":799,"date":"2018-11-14T16:11:20","date_gmt":"2018-11-14T16:11:20","guid":{"rendered":"https:\/\/barkhane.com\/php\/2018\/11\/14\/encapsulation-in-php\/"},"modified":"2024-03-03T14:56:32","modified_gmt":"2024-03-03T14:56:32","slug":"encapsulation-in-php","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/encapsulation-in-php\/","title":{"rendered":"Encapsulation in PHP"},"content":{"rendered":"<p>Encapsulation in PHP is Wrapping up data member and method together into a single unit (i.e. <a href=\"https:\/\/barkhane.com\/php\/classes-in-php\/\">Class<\/a>) is called Encapsulation.<br \/>\nEncapsulation in PHP\u00a0 is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.<br \/>\nEncapsulation is like your bag in which you can keep your pen, book etc. It means this is the property of encapsulating members and functions.<\/p>\n<div>\n<pre>class YourBag\n{\nbook();\/\/ calling function of book\npen();\/\/ calling function of pen\nReadBook ();\/\/ calling function readBook\n}\n<\/pre>\n<\/div>\n<p>Encapsulation means hiding the internal details of an object, i.e. how an object does something.<br \/>\nEncapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented.<br \/>\nEncapsulation is a technique used to protect the information in an object from the other object.<\/p>\n<p>Hide the data for security such as making the variables as private, and expose the property to access the private data which would be public.<br \/>\nSo, when you access the property you can validate the data and set it.<\/p>\n<p>Example:<\/p>\n<pre> &lt;?php\nclass YourMarks\n{\nprivate $mark;\npublic Marks\n{\nget { return $mark; }\nset { if ($mark &gt; 0) $mark = 10; else $mark = 0; }\n}\n}\n?&gt;\n<\/pre>\n<p>Below the real life example of encapsulation:<\/p>\n<p><strong>1. Take an example of Mobile Phone and it\u2019s Manufacture:<\/strong><\/p>\n<p>Let\u2019s take example of Mobile Phone and Mobile Phone Manufacturer<br \/>\nSuppose you are a Mobile Phone Manufacturer and you designed and developed a Mobile Phone design(class), now by using machinery you are manufacturing a Mobile Phone(object) for selling, when you sell your Mobile Phone the user only learn how to use the Mobile Phone but not that how this Mobile Phone works.<\/p>\n<p>This means that you are creating the class with function and by making object (capsule) of it you are making availability of the functionality of you class by that object and without the interference in the original class.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.\u00a0 another example of real life (daily use) that is \u201cTV operation\u201d. Many peoples operate TV in daily life.<\/strong><\/p>\n<p>It is encapsulated with cover and we can operate with remote and no need to open TV and change the channel.<br \/>\nHere everything is in private except remote so that anyone can access not to operate and change the things in TV.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Encapsulation in PHP is Wrapping up data member and method together into a single unit (i.e. Class) is called Encapsulation. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-799","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/comments?post=799"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/799\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}