1. 40
    Create class properties in previous versions of PHP
    2m 47s

Create class properties in previous versions of PHP

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Attaching properties to a PHP class is important, so you can track specific values of data entities or functionality.

Instructor: [0:00] We now have a value passed to the constructor, but a var_dump really won't be doing anything for us. We need to assign this value to a variable.

[0:10] In classes, you do this with something called a class property. You can think of classes as representing a specific entity, and properties representing values related to that entity.

[0:22] Just like in this example, the author is the entity, and the author name would be a good property. We can also have other properties like gender, age, location, and so on.

[0:35] This lesson covers class properties in versions of PHP that came before PHP 8.

[0:43] I included it first though because it will help you understand how scope and properties work within classes, or if you needed to work with legacy code that was created in versions before PHP 8.

[0:56] You can create a class property by using a scope keyword right at the start of a class.

[1:02] We will get into scopes in a bit more detail later on. For now, let's just create a new property with the public scope. This is the default scope for functions and properties. Next comes the name of the property and this will be a variable name. Let's just name this $name.

[1:25] You can also define a data type for this class property, which would go in between the scope and the name of the property. This is a string, so we can cast this as the string data type. Next we will update our constructor to assign it to this property.

[1:43] Within our constructor, we will first use the $this keyword. The $this keyword references the class that you are currently within. This is the specific instance of this author class, which is actually the author variable. Next we will use a dash and greater than sign which is referred to as an arrow.

[2:08] Next comes the property name, which in this case is name. This is derived from the name of our property without the dollar sign. Finally, we can just set this value equal to our argument name, which is $name, which comes from our argument.

[2:28] You can define as many properties as you like here by duplicating out the lines and then changing the names and value types of each property. You can also pass in multiple arguments and assign them to those property names, if you wish, depending on your needs.

egghead
egghead
~ 42 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today