Create and instantiate a class in PHP
Learn how to create classes and instantiate objects in PHP to organize related functionality.
Lesson Content
A great way to structure your code, once it gets more complex, is to use PHP classes. Classes allow you to organize and colocate multiple functions of business logic together into a unified entity.
Create a new file at classes/Author.php and add a new Author class. We will do this with the class keyword, followed by the name of the class we’d like to create, Author.
<?php declare(strict_types=1);
class Author
{
}
Now we will include that class with a require statement. Let’s require it in the functions.php file:
<?php declare(strict_types=1);
require('classes/Author.php');
function getPosts(): array
{
return [
[
'title' => 'How to learn PHP',
'content' => 'This is how you learn PHP.',
],
[
'title' => 'How to learn MySQL',
'content' => 'This is how you learn MySQL.',
],
[
'title' => 'How to learn Nginx',
'content' => 'This is how you learn Nginx.',
],
];
}
function getPostText(int $numPosts): string
{
return $numPosts === 1 ? 'post' : 'posts';
}
PHP object instantiation happens with a special PHP keyword, new. When we follow new with the class name we’d like to create, followed by parenthesis, when PHP parses this code, it will create a new object that is an instance of this class.
<?php declare(strict_types=1);
require('classes/Author.php');
function getPosts(): array
{
$author = new Author();
return [
[
'title' => 'How to learn PHP',
'content' => 'This is how you learn PHP.',
],
[
'title' => 'How to learn MySQL',
'content' => 'This is how you learn MySQL.',
],
[
'title' => 'How to learn Nginx',
'content' => 'This is how you learn Nginx.',
],
];
}
function getPostText(int $numPosts): string
{
return $numPosts === 1 ? 'post' : 'posts';
}
At this moment, this code is just creating a new instance of this object, and assigning it to the $author variable. This variable now holds a reference to this newly created object.
<?php declare(strict_types=1);
class Author
{
}
<?php declare(strict_types=1);
require('classes/Author.php');
function getPosts(): array
{
$author = new Author();
return [
[
'title' => 'How to learn PHP',
'content' => 'This is how you learn PHP.',
],
[
'title' => 'How to learn MySQL',
'content' => 'This is how you learn MySQL.',
],
[
'title' => 'How to learn Nginx',
'content' => 'This is how you learn Nginx.',
],
];
}
function getPostText(int $numPosts): string
{
return $numPosts === 1 ? 'post' : 'posts';
}
+<?php declare(strict_types=1);
+
+class Author
+{
+
+}
+
<?php declare(strict_types=1);
require('classes/Author.php');
function getPosts(): array
{
+ $author = new Author();
+
return [
[
'title' => 'How to learn PHP',
'content' => 'This is how you learn PHP.',
],
[
'title' => 'How to learn MySQL',
'content' => 'This is how you learn MySQL.',
],
[
'title' => 'How to learn Nginx',
'content' => 'This is how you learn Nginx.',
],
];
}
function getPostText(int $numPosts): string
{
return $numPosts === 1 ? 'post' : 'posts';
}
<?php
declare(strict_types=1);
class Foo
{
}
<?php
declare(strict_types=1);
require 'classes/Foo.php';
function createFoo(): Foo
{
return new Foo();
}
Join the discussion!
Comments
Want to comment on this lesson?
Log in to leave comments.