So in previous examples, everything was public.
Now in this video we're going to learn about encapsulation in object oriented design.
So let's create a class that would be called Bankaccount because that's a good example.
And first start with a private field that would be float.
And it would be balance initialized with zero.
So we haven't used the private visibility modifier yet.
So I need to explain that.
So this means that I can only access this field from within this bank account class, its methods,
and I can't access it from outside.
So if I'm going to create this object now also keep in mind that you don't need a constructor.
It's optional.
And I'd like to echo the account balance.
This would This would throw an error because it is private.
So let me run this.
And we've got a fatal error.
Cannot access a private property.
So without encapsulation.
And if the balance would be public, I could do all kind of weird stuff.
So this is a bank account.
And yet I would be able to set the balance at will at -400 maybe.
So this is not how bank account class should work.
It should give us functionality that is safe to use.
So we should either get the balance, we should deposit money and withdraw the money.
But when withdrawing you should not be able to go below the balance.
So that's why to do anything with a bank account, we're gonna use encapsulation by providing public
methods.
Okay, so let's create a get balance function.
This would be a so-called getter.
And it simply returns the balance.
So I do this balance.
That's the way I can access the current balance and echo it.
So maybe I'm just going to do var dump account get balance and I should get zero in this case.
There it is.
So now let's add some methods that will let us modify the balance.
Let's start with deposit where there is one argument called amount and it is void.
So it doesn't return any value.
Now here we can check if the amount is more than zero because we don't want to deposit negative values.
And if that's the case we can add this to the balance.
So this plus equals operator will just add the amount to balance.
So again if we wouldn't check if the amount is non-negative we could accidentally subtract money from
the balance when we try to deposit money.
Okay.
So now this is safe to use.
But something a little bit more complex is to withdraw the money from the account.
So this also accepts an amount and it can return a boolean.
So this would just mean whether it was successful or not.
Okay.
So first we need to make sure that the amount is More than zero.
Another thing is that we need to make sure that the actual balance is more or equal the amount to be
withdrawn.
If that's the case.
We just subtract the amount from the balance and we return true.
Otherwise we just return false which means the operation was not successful.
Okay.
So now let's do a couple of operations in here.
First on the account.
I'm going to deposit something.
Then on the account or from the account I'm going to withdraw 500.
And finally we're going to see what's the remaining deposit.
I expect 500.
And that's the case.