{"id":91,"date":"2021-03-08T00:17:06","date_gmt":"2021-03-08T00:17:06","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=91"},"modified":"2025-04-07T10:53:14","modified_gmt":"2025-04-07T10:53:14","slug":"php-objects","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-oop\/php-objects\/","title":{"rendered":"PHP Objects"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about PHP objects, how to define a class, and how to create an object from a class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-is-an-object'>What is an Object <a href=\"#what-is-an-object\" class=\"anchor\" id=\"what-is-an-object\" title=\"Anchor for What is an Object\">#<\/a><\/h2>\n\n\n\n<p>If you look at the world around you, you&#8217;ll find many examples of tangible objects: lamps, phones, computers, and cars. Also, you can find intangible objects such as bank accounts and transactions. <\/p>\n\n\n\n<p>All of these objects share the two common key characteristics:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>State<\/li>\n\n\n\n<li>Behavior<\/li>\n<\/ul>\n\n\n\n<p>For example, a bank account has the state that consists of:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Account number <\/li>\n\n\n\n<li>Balance<\/li>\n<\/ul>\n\n\n\n<p>A bank account also has the following behaviors:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Deposit<\/li>\n\n\n\n<li>Withdraw<\/li>\n<\/ul>\n\n\n\n<p>PHP objects are conceptually similar to real-world objects because they consist of state and behavior.<\/p>\n\n\n\n<p>An object holds its state in <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variables\/\">variables<\/a> that are often referred to as <strong>properties<\/strong>. An object also exposes its behavior via <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-functions\/\">functions<\/a> which are known as <strong>methods<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-is-a-class'>What is a class? <a href=\"#what-is-a-class\" class=\"anchor\" id=\"what-is-a-class\" title=\"Anchor for What is a class?\">#<\/a><\/h2>\n\n\n\n<p>In the real world, you can find many same kinds of objects. For example, a bank has many bank accounts. All of them have account numbers and balances.<\/p>\n\n\n\n<p>These bank accounts are created from the same blueprint. In object-oriented terms, we say that an individual bank account is an instance of a Bank Account class.<\/p>\n\n\n\n<p>By definition, a class is the blueprint of objects. For example, from the Bank Account class, you can create many bank account objects.<\/p>\n\n\n\n<p>The following illustrates the relationship between the <code>BankAccount<\/code> class and its objects. From the <code>BankAccount<\/code> class you can create many <code>BankAccount<\/code> objects. And each object has its own account number and balance.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"228\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/03\/PHP-Objects.png\" alt=\"\" class=\"wp-image-610\" srcset=\"https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/03\/PHP-Objects.png 367w, https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/03\/PHP-Objects-300x186.png 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id='define-a-class'>Define a class <a href=\"#define-a-class\" class=\"anchor\" id=\"define-a-class\" title=\"Anchor for Define a class\">#<\/a><\/h2>\n\n\n\n<p>To define a class, you specify the <code>class<\/code> keyword followed by a name like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ClassName<\/span>\n<\/span>{\n    <span class=\"hljs-comment\">\/\/...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following defines a new class called <code>BankAccount<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIEJhbmtBY2NvdW50CnsKfQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>By convention, you should follow these rules when defining a class:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A class name should be in the upper camel case where each word is capitalized. For example, <code>BankAccount<\/code>, <code>Customer<\/code>, <code>Transaction<\/code>, and <code>DebitNote<\/code>.<\/li>\n\n\n\n<li>If a class name is a noun, it should be in the singular noun. <\/li>\n\n\n\n<li>Define each class in a separate PHP file.<\/li>\n<\/ul>\n\n\n\n<p>From the <code>BankAccount<\/code> class, you can create a new bank account object by using the <code>new<\/code> keyword like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n}\n\n$account = <span class=\"hljs-keyword\">new<\/span> BankAccount();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIEJhbmtBY2NvdW50CnsKfQoKJGFjY291bnQgPSBuZXcgQmFua0FjY291bnQoKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this syntax, the <code>$account<\/code> is a variable that references the object created by the <code>BankAccount<\/code> class. The parentheses that follow the <code>BankAccount<\/code> class name are optional. Therefore, you can create a new <code>BankAccount<\/code> object like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$account = <span class=\"hljs-keyword\">new<\/span> BankAccount;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The process of creating a new object is also called instantiation. In other words, you instantiate an object from a class. Or you create a new object from a class.<\/p>\n\n\n\n<p>The <code>BankAccount<\/code> class is empty because it doesn&#8217;t have any state and behavior.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='add-properties-to-a-class'>Add properties to a class <a href=\"#add-properties-to-a-class\" class=\"anchor\" id=\"add-properties-to-a-class\" title=\"Anchor for Add properties to a class\">#<\/a><\/h2>\n\n\n\n<p>To add properties to the <code>BankAccount<\/code> class, you place <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variables\/\">variables<\/a> inside it. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">public<\/span> $accountNumber;\n    <span class=\"hljs-keyword\">public<\/span> $balance;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIEJhbmtBY2NvdW50CnsKICAgIHB1YmxpYyAkYWNjb3VudE51bWJlcjsKICAgIHB1YmxpYyAkYmFsYW5jZTsKfQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The <code>BankAccount<\/code> class has two properties <code>$accountNumber<\/code> and <code>$balance<\/code>. In front of each property, you see the <code>public<\/code> keyword. <\/p>\n\n\n\n<p>The <code>public<\/code> keyword determines the <a href=\"https:\/\/phptutorial.net\/php-oop\/php-access-modifiers\/\">visibility of a property<\/a>. In this case, you can access the property from the outside of the class.<\/p>\n\n\n\n<p>To access a property, you use the object operator  (<code>-&gt;<\/code>) like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$object-&gt;property;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example shows how to set the values of the <code>accountNumber<\/code> and <code>balance<\/code> properties:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">public<\/span> $accountNumber;\n    <span class=\"hljs-keyword\">public<\/span> $balance;\n}\n\n$account = <span class=\"hljs-keyword\">new<\/span> BankAccount();\n\n$account-&gt;accountNumber = <span class=\"hljs-number\">1<\/span>;\n$account-&gt;balance = <span class=\"hljs-number\">100<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"The bank account $account-&gt;accountNumber has a balance of $$account-&gt;balance\"<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIEJhbmtBY2NvdW50CnsKICAgIHB1YmxpYyAkYWNjb3VudE51bWJlcjsKICAgIHB1YmxpYyAkYmFsYW5jZTsKfQoKJGFjY291bnQgPSBuZXcgQmFua0FjY291bnQoKTsKCiRhY2NvdW50LT5hY2NvdW50TnVtYmVyID0gMTsKJGFjY291bnQtPmJhbGFuY2UgPSAxMDA7CgplY2hvICJUaGUgYmFuayBhY2NvdW50ICRhY2NvdW50LT5hY2NvdW50TnVtYmVyIGhhcyBhIGJhbGFuY2Ugb2YgJCRhY2NvdW50LT5iYWxhbmNlIjs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Besides the <code>public<\/code> keyword, PHP also has <code>private<\/code> and <code><a href=\"https:\/\/phptutorial.net\/php-oop\/php-protected\/\">protected<\/a><\/code> keywords which you&#8217;ll learn in the <a href=\"https:\/\/phptutorial.net\/php-oop\/php-access-modifiers\/\" target=\"_blank\" rel=\"noreferrer noopener\">access modifiers tutorial<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='add-methods-to-a-class'>Add methods to a class <a href=\"#add-methods-to-a-class\" class=\"anchor\" id=\"add-methods-to-a-class\" title=\"Anchor for Add methods to a class\">#<\/a><\/h2>\n\n\n\n<p>The following shows the syntax for defining a method in a class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ClassName<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">methodName<\/span><span class=\"hljs-params\">(parameter_list)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-comment\">\/\/ implementation<\/span>\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Like a property, a method also has one of the three visibility modifiers: <code>public<\/code>, <code>private<\/code>, and <code>protected<\/code>. If you define a method without any visibility modifier, it defaults to <code>public<\/code>. <\/p>\n\n\n\n<p>The following example defines the <code>deposit()<\/code> method for the <code>BankAccount<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">public<\/span> $accountNumber;\n\n    <span class=\"hljs-keyword\">public<\/span> $balance;\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">deposit<\/span><span class=\"hljs-params\">($amount)<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> ($amount &gt; <span class=\"hljs-number\">0<\/span>) {\n            <span class=\"hljs-keyword\">$this<\/span>-&gt;balance += $amount;\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIEJhbmtBY2NvdW50CnsKICAgIHB1YmxpYyAkYWNjb3VudE51bWJlcjsKCiAgICBwdWJsaWMgJGJhbGFuY2U7CgogICAgcHVibGljIGZ1bmN0aW9uIGRlcG9zaXQoJGFtb3VudCkKICAgIHsKICAgICAgICBpZiAoJGFtb3VudCA-IDApIHsKICAgICAgICAgICAgJHRoaXMtPmJhbGFuY2UgKz0gJGFtb3VudDsKICAgICAgICB9CiAgICB9Cn0\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The <code>deposit()<\/code> method accepts an argument <code>$amount<\/code>. It checks if the <code>$amount<\/code> is greater than zero before adding it to the balance.<\/p>\n\n\n\n<p>To call a method, you also use the object operator (<code>-&gt;<\/code>) as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$object-&gt;method(arguments)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The new syntax in the <code>deposit()<\/code> method is the <code><a href=\"https:\/\/phptutorial.net\/php-oop\/php-this\/\">$this<\/a><\/code> variable. The <code>$this<\/code> variable is the current object of the <code>BankAccount<\/code> class. <\/p>\n\n\n\n<p>For example, when you create a new object <code>$account<\/code> and call the <code>deposit()<\/code> method, the <code>$this<\/code> inside the method is the <code>$account<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$account = <span class=\"hljs-keyword\">new<\/span> BankAccount();\n\n$account-&gt;accountNumber = <span class=\"hljs-number\">1<\/span>;\n$account-&gt;balance = <span class=\"hljs-number\">100<\/span>;\n\n$account-&gt;deposit(<span class=\"hljs-number\">100<\/span>);\n\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"The bank account $account-&gt;accountNumber has a balance of $$account-&gt;balance\"<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIEJhbmtBY2NvdW50CnsKICAgIHB1YmxpYyAkYWNjb3VudE51bWJlcjsKCiAgICBwdWJsaWMgJGJhbGFuY2U7CgogICAgcHVibGljIGZ1bmN0aW9uIGRlcG9zaXQoJGFtb3VudCkKICAgIHsKICAgICAgICBpZiAoJGFtb3VudCA-IDApIHsKICAgICAgICAgICAgJHRoaXMtPmJhbGFuY2UgKz0gJGFtb3VudDsKICAgICAgICB9CiAgICB9Cn0KCgokYWNjb3VudCA9IG5ldyBCYW5rQWNjb3VudCgpOwoKJGFjY291bnQtPmFjY291bnROdW1iZXIgPSAxOwokYWNjb3VudC0-YmFsYW5jZSA9IDEwMDsKCiRhY2NvdW50LT5kZXBvc2l0KDEwMCk7CgplY2hvICJUaGUgYmFuayBhY2NvdW50IHskYWNjb3VudC0-YWNjb3VudE51bWJlcn0gaGFzIGEgYmFsYW5jZSBvZiB7JGFjY291bnQtPmJhbGFuY2V9VVNEIjs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Similarly, you can add the <code>withdraw()<\/code> method to the <code>BankAccount<\/code> class as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> $accountNumber;\n\n\t<span class=\"hljs-keyword\">public<\/span> $balance;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">deposit<\/span><span class=\"hljs-params\">($amount)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">if<\/span> ($amount &gt; <span class=\"hljs-number\">0<\/span>) {\n\t\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;balance += $amount;\n\t\t}\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">withdraw<\/span><span class=\"hljs-params\">($amount)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">if<\/span> ($amount &lt;= <span class=\"hljs-keyword\">$this<\/span>-&gt;balance) {\n\t\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;balance -= $amount;\n\t\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">true<\/span>;\n\t\t}\n                <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">false<\/span>;\n\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIEJhbmtBY2NvdW50CnsKCXB1YmxpYyAkYWNjb3VudE51bWJlcjsKCglwdWJsaWMgJGJhbGFuY2U7CgoJcHVibGljIGZ1bmN0aW9uIGRlcG9zaXQoJGFtb3VudCkKCXsKCQlpZiAoJGFtb3VudCA-IDApIHsKCQkJJHRoaXMtPmJhbGFuY2UgKz0gJGFtb3VudDsKCQl9Cgl9CgoJcHVibGljIGZ1bmN0aW9uIHdpdGhkcmF3KCRhbW91bnQpCgl7CgkJaWYgKCRhbW91bnQgPD0gJHRoaXMtPmJhbGFuY2UpIHsKCQkJJHRoaXMtPmJhbGFuY2UgLT0gJGFtb3VudDsKCQkJcmV0dXJuIHRydWU7CgkJfQogICAgICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwoKCX0KfQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The <code>withdraw()<\/code> method checks the current balance.<\/p>\n\n\n\n<p>If the balance is less than the withdrawal amount, the <code>withdraw()<\/code> method returns <code>false<\/code>. <\/p>\n\n\n\n<p>Later, you&#8217;ll learn how to throw an exception instead. Otherwise, it deducts the withdrawal amount from the balance and returns <code>true<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Objects have states and behaviors.<\/li>\n\n\n\n<li>A class is a blueprint for creating objects.<\/li>\n\n\n\n<li>Properties represent the object&#8217;s state, and methods represent the object&#8217;s behavior. Properties and methods have visibility.<\/li>\n\n\n\n<li>Use the <code>new<\/code> keyword to create an object from a class.<\/li>\n\n\n\n<li>The <code>$this<\/code> variable references the current object of the class.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"91\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-objects\/\"\n\t\t\t\tdata-post-title=\"PHP Objects\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"91\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-objects\/\"\n\t\t\t\tdata-post-title=\"PHP Objects\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about PHP objects, how to define a clas, and how to create an object from a class.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1753,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-91","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/91\/revisions"}],"predecessor-version":[{"id":3216,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/91\/revisions\/3216"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1753"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}