In short, a class is a code template used to generate one or more objects And a class is an entity that determines how an objects will behave and what the objects will contain, in other words it's a blueprint to build to build a specific type of objects

You declare a class with the class keyword and an followed by the class name. Class names can be any combination of numbers and letters, although they must not begin with a number. The code associated with a class must be enclosed within braces.

<?php
class ShopProduct
{    
    // class body
}

Setting Properties in a Class

Classes can define special variables called properties. A property, also known as a member variable, holds data that can common from object to object. So in the case of ShopProduct objects, you may wish to manipulate title and price fields, for example.A property in a class looks similar to a standard variable except that, in declaring a property, you must precede the property variable with a visibility keyword. This can be public, protected, or private, and it determines the scope from which the property can be accessed.

<?php
class ShopProduct
{    
    public $title = "default product";
    public $producerMainName = "main name";
    public $producerFirstName = "first name";    
    public $price  = 0;
}

As you can see, I set up four properties, assigning a default value to each of them. Any objects I instantiate from the ShopProduct class will now be deal with default data. The public keyword in each property declaration ensures that I can access the property from outside of the object context.You can access property variables on an object-by-object basis using the characters '->' (the object operator) in conjunction with an object variable and property name, like this:

$product1 = new ShopProduct();
print $product1->title;

Because the properties are defined as public, you can assign values to them just as you can read them, replacing any default value set in the class:

$product1 = new ShopProduct();
$product2 = new ShopProduct();
$product1->title="My Antonia";
$product2->title="Catch 22";

In fact, PHP does not force us to declare all our properties in the class. You could add properties dynamically to an object, like this:

<?php
$product1->arbitraryAddition = "treehouse";

However, this method of assigning properties to objects is not considered good practice in object-oriented programming. Why is it bad practice to set properties dynamically? When you create a class you define a type. You inform the world that your class (and any object instantiated from it) consists of a particular set of fields and functions. If your ShopProduct class defines a $title property, then any code that works with ShopProduct objects can proceed on the assumption that a $title property will be available. There can be no guarantees about properties that have been dynamically set, though. My objects are still hard at this stage. When I need to work with an object’s properties, I must currently do so from outside the object. I reach in to set and get property information. Setting multiple properties on multiple objects will soon become a chore.


This post is part of series:

1 - Object Oriented Programming Concept2 - Classes3 - Objects4 - Methods5 - Constructors6 - Arguments and Types7 - Static Methods and Properties8 - Constant Properties9 - Abstract Classes10 - Interfaces11 - Traits
PHP#PHPOOP#OOP

Comments (0)