1. 42
    Retrieve public class properties with PHP’s arrow syntax
    1m 43s

Retrieve public class properties with PHP’s arrow syntax

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Creating class properties is useless unless you can read back that property! There's an easy way to do this, with PHP's shorthand arrow syntax.

Instructor: [0:00] We already created a constructor in this Author class which executes when a class is created or instantiated. Since this name class property has a public visibility, we can refer to it directly with a special arrow syntax on the created object instance.

[0:20] Let's go back to our functions.php file and we will name this variable author1. We will actually create a second instance of the author object, and let's name this Betsy Sue.

[0:36] We can now assign properties of these created objects to new author indexes on the POST array. Let's go to this first array element and we will create a new author index. This will be assigned a value of author1 => $name. We can reference this name class property directly since it is public in scope. Let's do the same thing for this second blog article. In the third one, we will reference author2's name.

[1:14] We want to display the author on our blog post listing page. Let's go back to the index.php file, and let's create a new line for the byline. We will refer to the author index that we just created.

[1:32] When we refresh the page, we will see that it grabs the instance of the name class property from both the newly created PHP objects.