What is encapsulation with simple example in php?

前端 未结 14 1482
谎友^
谎友^ 2020-12-08 02:27

What is encapsulation with simple example in php?

14条回答
  •  爱一瞬间的悲伤
    2020-12-08 03:11

    Simply I prefer that is visibility of your class's property and method. For example- - public - private - protected

    Let's take a look real life example for encapsulation.

     class MyClass{
        private $name;
    
        public function showName($newName){
            $this->name = $newName;
            return $this->name;
        }
     }
    
    
     //instantiate object
    
     $obj = new MyClass();
     echo $obj->showName("tisuchi");
    

    In this case, encapsulation means, we restrict some properties. Like, name property, we can not access from outside of the class. On the other hand, we can access public function entitled showName() with one private parameter.

    Simply what I prefer of encapsulation is-

    visibility of your property and method.

    Although, if you have any intension to understand encapsulation further, I refer to my special tutorial based on encapsulation.

    http://tisuchi.com/object-oriented-php-part-3-encapsulation-php/

    Hope, it will make your concept more clear. Have fun!

提交回复
热议问题