PHP __construct (?‍♂️Lesson 1: PHP Magic Methods)

Today, we’ll quickly cover the most popular PHP magic method – __construct.

__construct magic method

PHP allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on ea…


This content originally appeared on DEV Community and was authored by Clean Code Studio

Today, we'll quickly cover the most popular PHP magic method - __construct.

__construct magic method

PHP allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

Using the PHP __construct magic method

<?php

class User
{
    public $name;

    public function __construct($name)
    {
        $this->name = $name;
    }
}

$timmy = new User('Timmy');


echo $timmy->name;

// "Timmy"
.ltag__user__id__386677 .follow-action-button { background-color: #339d15 !important; color: #ffffff !important; border-color: #339d15 !important; }
cleancodestudio image

__construct PHP Magic Method

.ltag__user__id__386677 .follow-action-button { background-color: #339d15 !important; color: #ffffff !important; border-color: #339d15 !important; }
cleancodestudio image

Did you know I have a newsletter? ?

If you want to get notified when I publish new blog posts or make major project announcements, head over to

https://cleancodestudio.paperform.co/


This content originally appeared on DEV Community and was authored by Clean Code Studio


Print Share Comment Cite Upload Translate Updates
APA

Clean Code Studio | Sciencx (2021-07-28T17:04:55+00:00) PHP __construct (?‍♂️Lesson 1: PHP Magic Methods). Retrieved from https://www.scien.cx/2021/07/28/php-__construct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-1-php-magic-methods/

MLA
" » PHP __construct (?‍♂️Lesson 1: PHP Magic Methods)." Clean Code Studio | Sciencx - Wednesday July 28, 2021, https://www.scien.cx/2021/07/28/php-__construct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-1-php-magic-methods/
HARVARD
Clean Code Studio | Sciencx Wednesday July 28, 2021 » PHP __construct (?‍♂️Lesson 1: PHP Magic Methods)., viewed ,<https://www.scien.cx/2021/07/28/php-__construct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-1-php-magic-methods/>
VANCOUVER
Clean Code Studio | Sciencx - » PHP __construct (?‍♂️Lesson 1: PHP Magic Methods). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/28/php-__construct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-1-php-magic-methods/
CHICAGO
" » PHP __construct (?‍♂️Lesson 1: PHP Magic Methods)." Clean Code Studio | Sciencx - Accessed . https://www.scien.cx/2021/07/28/php-__construct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-1-php-magic-methods/
IEEE
" » PHP __construct (?‍♂️Lesson 1: PHP Magic Methods)." Clean Code Studio | Sciencx [Online]. Available: https://www.scien.cx/2021/07/28/php-__construct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-1-php-magic-methods/. [Accessed: ]
rf:citation
» PHP __construct (?‍♂️Lesson 1: PHP Magic Methods) | Clean Code Studio | Sciencx | https://www.scien.cx/2021/07/28/php-__construct-%f0%9f%a7%99%e2%80%8d%e2%99%82%ef%b8%8flesson-1-php-magic-methods/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.