Assign default property value : Class Property « Class « PHP






Assign default property value

<?php
    class Employee {
        public $title       = "product";
        public $mainName    = "main";
        public $firstName   = "first";
        public $price       = 0;
        
        function getFullName() {
            return "{$this->firstName}".
                   " {$this->mainName}";
        }
    }

$product1 = new Employee();
$product1->title = "title";
$product1->mainName  = "A";
$product1->firstName = "B";
$product1->price = 5.99;

print "author: ".$product1->getFullName()."\n";
?>


           
       








Related examples in the same category

1.Changing the Value of a Property from Within a Method
2.Define boolean Class properties
3.Accessing a Property from Within a Method
4.Adding Properties
5.Adding the $age variable to Cat
6.Changing the Value of a Property from Within a Method
7.Class including a complete collection of get and set methods.
8.Controlling Access to Class Members
9.Define a setter and getter