What is encapsulation with simple example in php?

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

What is encapsulation with simple example in php?

相关标签:
14条回答
  • 2020-12-08 02:53

    People seem to be mixing up details of object orientation with encapsulation, which is a much older and wider concept. An encapsulated data structure

    • can be passed around with a single reference, eg increment(myDate) rather than increment(year,month,day)
    • has a set of applicable operations stored in a single program unit (class, module, file etc)
    • does not allow any client to see or manipulate its sub-components EXCEPT by calling the applicable operations

    You can do encapsulation in almost any language, and you gain huge benefits in terms of modularisation and maintainability.

    0 讨论(0)
  • 2020-12-08 02:53

    In basic terms, it’s the way we define the visibility of our properties and methods. When you’re creating classes, you have to ask yourself what properties and methods can be accessed outside of the class. Let’s say we had a property named foo. If a class extends your class, is it allowed to manipulate and access foo? What if someone creates an instances of your class? Are they allowed to manipulate and access foo?

    0 讨论(0)
  • 2020-12-08 02:54

    Encapsulation is a way of storing an object or data as a property within another object, so that the outer object has full control over what how the internal data or object can be accessed.

    For example

    class OuterClass
    {
      private var $innerobject;
    
      function increment()
      {
        return $this->innerobject->increment();
      }
    }
    

    You have an extra layer around the object that is encapsulated, which allows the outer object to control how the inner object may be accessed. This, in combination with making the inner object/property private, enables information hiding.

    0 讨论(0)
  • 2020-12-08 02:57

    Encapsulation is just wrapping some data in an object. The term "encapsulation" is often used interchangeably with "information hiding". Wikipedia has a pretty thorough article.

    Here's an example from the first link in a Google search for 'php encapsulation':

    <?php
    
    class App {
         private static $_user;
    
         public function User( ) {
              if( $this->_user == null ) {
                   $this->_user = new User();
              }
              return $this->_user;
         }
    
    }
    
    class User {
         private $_name;
    
         public function __construct() {
              $this->_name = "Joseph Crawford Jr.";
         }
    
         public function GetName() {
              return $this->_name;
         }
    }
    
    $app = new App();
    
    echo $app->User()->GetName();
    
    ?>
    
    0 讨论(0)
  • 2020-12-08 02:57

    Wrapping up data member and method together into a single unit (i.e. Class) is called Encapsulation. Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object. Encapsulation is like your bag in which you can keep your pen, book etc. It means this is the property of encapsulating members and functions.

    <?php
     class YourMarks
      {
       private $mark;
       public Marks
      {
       get { return $mark; }
       set { if ($mark > 0) $mark = 10; else $mark = 0; }
      }
      }
    ?>
    

    I am giving an another example of real life (daily use) that is “TV operation”. Many peoples operate TV in daily life.

    It is encapsulated with cover and we can operate with remote and no need to open TV and change the channel. Here everything is in private except remote so that anyone can access not to operate and change the things in TV.

    0 讨论(0)
  • 2020-12-08 02:57

    Encapsulation is just how you wanted your objects/methods or properties/variables to be visible in your application. for example, :

    class ecap {
    
    public $name;
    
    private $id;
    
    protected $tax;
    
    }
    

    If you want to access private or protected properties, then you have to use getter and setter methods in your class that will be accessible from outside of your class. Meaning, you cannot access your private or protected properties directly from outside of your class but you can use through any methods. Let’s take a look-

    in the class, add the following method:

    class ecap 
    {
     public function userId(){
     return $this->id;
     }
    } 
    

    and we can access it like:

     $obj = new ecap();
     echo $obj->userId();
    
    0 讨论(0)
提交回复
热议问题