Cara menggunakan php encapsulation w3schools

In this HTML tutorial, you will find more than 200 examples. With our online "Try it Yourself" editor, you can edit and test each example yourself!

Go to HTML Examples!


Place for your advertisement!

HTML Exercises

This HTML tutorial also contains nearly 100 HTML exercises.

Test Yourself With Exercises

Exercise:

Add a "tooltip" to the paragraph below with the text "About W3Schools".

<p ="About W3Schools">W3Schools is a web developer's site.</p>


Submit Answer »


HTML Quiz Test

Test your HTML skills with our HTML Quiz!

Start HTML Quiz!


Place for your advertisement!

HTML References

At W3Schools you will find complete references about HTML elements, attributes, events, color names, entities, character-sets, URL encoding,language codes, HTTP messages, browser support, and more:

Cara menggunakan php encapsulation w3schools
  • Encapsulation is a concept where we encapsulate all the data and member functions together to form an object.
  • Wrapping up data member and method together into a single unit is called Encapsulation.
  • Encapsulation also allows a class to change its internal implementation without hurting the overall functioning of the system.
  • Binding the data with the code that manipulates it.
  • It keeps the data and the code safe from external interference.

Example 1

Output:

Cara menggunakan php encapsulation w3schools

Object Oriented Programming is a software approach added to PHP5, which helps in building the composite application in an easy way. Some of the OOP concepts added into the PHP5 are an abstraction, interface, static method, and static class, etc...

In this article, we will learn Encapsulation and it's implementation through a few examples.

The wrapping up of data and methods into a single unit (called class) is known as encapsulation. Encapsulation is a protection mechanism for the data members and methods present inside the class. In the encapsulation technique, we are restricting the data members from access to outside world end-user.

In PHP, encapsulation utilized to make the code more secure and robust. Using encapsulation, we are hiding the real implementation of data from the user and also does not allow anyone to manipulate data members except by calling the desired operation.

Example

Let' understand this through an example.

CheckBalance(10005285637,1**3);
?>

Explanation:

In this example, all the ATM class data members (variable) are marked with the private modifier. It implies that we can not directly access ATM class data members (property). So, we can't change the class property directly. The only approach to change the class property (data members) is calling a method (function). That’s the reason we have stated all the ATM class methods with a public access modifier. The user can pass the expected arguments to a class method to perform a particular task. 

Suppose anyone wants to check balance then he needs to access the CheckBalance() method with the required arguments $custid="10005285637" and $atmpin="1**3". This is called Data hiding through Encapsulation.

In today’s technical world, maintaining privacy has become one of the demanding needs for the protection of important data. Whenever data modified in one function affects the other functions, it causes a lot of problems in any software. To overcome this problem, object-oriented programming in PHP uses the concept of Encapsulation.

So the OOPs concept of Encapsulation in PHP means, enclosing the internal details of the object to protect from external sources. It describes, combining the class, data variables and member functions that work on data together within a single unit to form an object. Otherwise, its the bundling of properties and behavior in a single class unit.

Data is not accessed directly, in fact, they are accessed through functions(GET or SET) written inside the class. Attributes are kept private but getter(GET) and setter(SET) methods are kept public for manipulation of these attributes.

PHP program for encapsulation: The methods or functions in the following program are update password and check the course name. GFG class defines all the operations related to GFG users.




<?php

  

// PHP program to implements encapsulation

class GFG {

  

    private

First name is set to Meena
Gender is set to Female
0
First name is set to Meena
Gender is set to Female
1

    private

First name is set to Meena
Gender is set to Female
4
First name is set to Meena
Gender is set to Female
1

First name is set to Meena
Gender is set to Female

    

First name is set to Meena
Gender is set to Female
8

    <?php0 <?php1 <?php2

First name is set to Meena
Gender is set to Female
0<?php4
First name is set to Meena
Gender is set to Female
4<?php6

<?php

<?php8<?php9

<?php8 1 2 3

 4 5

First name is set to Meena
Gender is set to Female
4  5 8  5
First name is set to Meena
Gender is set to Female
0// PHP program to implements encapsulation1

<?php

<?php8 1 // PHP program to implements encapsulation5

First name is set to Meena
Gender is set to Female
1

    // PHP program to implements encapsulation8

  

    class1

    <?php0 <?php1 class5

First name is set to Meena
Gender is set to Female
0<?php6

<?php

<?php8<?php9

<?php8 1 2GFG {4

 4 5

First name is set to Meena
Gender is set to Female
0// PHP program to implements encapsulation1

<?php

<?php8 1 // PHP program to implements encapsulation5

First name is set to Meena
Gender is set to Female
1

    // PHP program to implements encapsulation8

// PHP program to implements encapsulation8

  

 8  9    0     1

 8     3    4<?php4    6// PHP program to implements encapsulation1

 8     9private0// PHP program to implements encapsulation1

  

private3

Output:

Function to update password 'geeks54321' for user GFG12
Function to check course name of user GFG06

Note: The data members and class properties are not accessible to the outer end-user. So they cannot change the properties.

Program to Access Variables




<?php

private

class private7

    private

First name is set to Meena
Gender is set to Female
00
First name is set to Meena
Gender is set to Female
1

    private

First name is set to Meena
Gender is set to Female
04
First name is set to Meena
Gender is set to Female
1

private

    <?php0 <?php1

First name is set to Meena
Gender is set to Female
10

<?php8

First name is set to Meena
Gender is set to Female
12
First name is set to Meena
Gender is set to Female
13
First name is set to Meena
Gender is set to Female
14

    // PHP program to implements encapsulation8

private

    <?php0 <?php1

First name is set to Meena
Gender is set to Female
21
First name is set to Meena
Gender is set to Female
00<?php6

<?php8

First name is set to Meena
Gender is set to Female
13
First name is set to Meena
Gender is set to Female
26
First name is set to Meena
Gender is set to Female
00
First name is set to Meena
Gender is set to Female
1

<?php8 1 2

First name is set to Meena
Gender is set to Female
32 5
First name is set to Meena
Gender is set to Female
00// PHP program to implements encapsulation1

<?php8 1 2// PHP program to implements encapsulation5// PHP program to implements encapsulation1

    // PHP program to implements encapsulation8

private

    <?php0 <?php1

First name is set to Meena
Gender is set to Female
47

<?php8

First name is set to Meena
Gender is set to Female
12
First name is set to Meena
Gender is set to Female
13
First name is set to Meena
Gender is set to Female
51

    // PHP program to implements encapsulation8

private

    <?php0 <?php1

First name is set to Meena
Gender is set to Female
58
First name is set to Meena
Gender is set to Female
04<?php6

<?php8

First name is set to Meena
Gender is set to Female
62  2
First name is set to Meena
Gender is set to Female
64
First name is set to Meena
Gender is set to Female
65
First name is set to Meena
Gender is set to Female
04
First name is set to Meena
Gender is set to Female
67
First name is set to Meena
Gender is set to Female
68
First name is set to Meena
Gender is set to Female
65
First name is set to Meena
Gender is set to Female
04<?php6