Lesson 10 - Static class members in PHP pt. 2 - constants

PHP OOP Static class members in PHP pt. 2 - constants

In the previous tutorial, Static class members in PHP, we introduced you all to static members and went over how to use them. In today's lesson, we will stick to the same topic and go deeper into detail with static members.

Password validation

Let's keep on working on our previous example. Imagine that our people had to be registered in an informational system. Other than their name, they would also need a password. Before creating a Human instance, we would have to validate the password that had been entered, to make sure it's not too short.

The password length validation method logically belongs to the Human class. However, at the moment that we would need to validate the password (most likely when a form is submitted), we wouldn't have a Human instance. However, in order to create a Human instance, we would need to know his/her first and last name, age and password (as these are required in the Human class' constructor). However, in order to call the password validation method, we need a Human instance. We'll work around this issue by making the password validation method static. This way, we will be able to call it without having to create an instance while still having it in the Human class, where it belongs.

This particular validation approach is simple, ideally, we'd have to make sure the password doesn't contain accent characters, etc. We will add a $password instance variable to the Human class. Hopefully, you understand why the $password variable is private.

private $password;

Now, modify the constructor so that users can only be created when a password is entered:

public function __construct($firstName, $lastName, $age, $password)
        $this->firstName = $firstName;
        $this->lastName = $lastName;
        $this->age = $age;
        $this->id = self::$peopleCount;
        $this->password = $password;

Modify PhpProgrammer's constructor from the previous lessons in a similar way because we will need it later on.

The password would also have to be hashed somehow, to keep it simple, though, we'll put that sort of thing off to the side for the next couple of courses. Now, let's add the public static validPassword() method:

public static function validPassword($password)
        return (mb_strlen($password) >= 5);

The method returns true when the password length is greater than 5 characters, otherwise, it returns false. Other than the use of mb_strlen(), there really is nothing particularly interesting about this method. As you may know, mb_strlen() supports UTF-8, unlike its outdated relative strlen(). In other words, in order to be able to use it, we'll have to set the encoding in our document. Let's add it somewhere at the very beginning:


For more information, refer to the official PHP manual.

Now let's get our user registration going in the main document:

$password = 'passwordissword';
if (Human::validPassword($password))
        $human = new Human('John', 'Smith', 32, $password);
        echo("Password OK");
        echo('Password must be at least 5 characters long');

We can call the validation method without even having to create an instance, which is really handy in this case.


Constants are a type of static class members. As you already know from math classes, constants are unchangeable values. Constants are always static, i.e. they don't require the static modifier. We declare them using the "const" keyword, so they always belong to a certain class. We name them in UPPER_CASE and use underscores instead of spaces. Also, it's best to declare them at the very beginning of a class, even before properties.

Constant Pi

Let's add the constant Pi to our Math.php class that we made in the previous lesson:

const PI = 3.14159265358979;

Now, let's go ahead and use it in index.php:

$radius = 5;
$area = Math::PI * Math::square($radius);
echo("Circle area is $area cm<sup>2</sup>.");

The result:

Your page

Minimum password length constant

Constants come in handy for storing things like minimum password lengths and whatnot. If we ever decide to change it, or wanted to use it somewhere else, we would be able to because it is static and only defined once. Now let's add the PASSWORD_LENGTH constant into our Human.php class, with its value set to 5:


Now, replace the 5 in the validPassword() method with the constant we just defined. Since it is a static class member, the only way to access it from inside the class is using the "self" keyword:

public static function validPassword($password)
        return (mb_strlen($password) >= self::MIN_PASSWORD_LENGTH);

Now the program works as it did before. Constants are accessible from outside of the class as well:

echo('Welcome to the user registration page, pick a user name and password of at least ' . Human::MIN_PASSWORD_LENGTH . ' characters');

We use constants every time we define a value that should not change during runtime. Another common use for constants is setting limits, i.e. maximum number of member events:

const MAX_EVENTS = 50;

Enumeration types (ENUM) in PHP

Constants in PHP are also often used to replace enumerated values, which PHP does not support. Although we could make a class that mimicks them, using constants is best in this case, because they are much simpler.

When you have a variable that can only consist of a single value from a specific enumerated list, use constants.

Message type

To get a better feel of what enumerated values are and do, we'll make a short little example program. We will use them when storing a message that generated by our content management system. The message will either be informative (blue with an "i" icon), an error (red with cross icon) or successful (green with tick).

We'll use an associative array to represent the message, where the key is the message type and the value is the message text. Without any knowledge of constants, we'd be forced to use a string to store the message type:

$message = array(
        'type' => 'success',
        'text' => 'Article has been successfully published',

Later on, we'll assign a CSS class to each message type, as well as sort them by type. Since we want to assign the right message text simple and clear, we'll make constants enumerated values. Constants are declared at the very beginning of a class (our IDE will even autocomplete them for us).

class MessageManager
        const TYPE_INFO = 0;
        const TYPE_SUCCESS = 1;
        const TYPE_ERROR = 2;

        public function addMessage($type, $text)
                . . .

Everything is much clearer now. In the rough draft above, the addMessage() method creates an array with the message, and stores it afterward. I completely left out the inner mechanism to keep the lesson simple. We would then be able to call the addMessage() method like this:

$messageManager = new MessageManager();
$messageManager->addMessage(MessageManager::TYPE_SUCCESS, 'Article has been successfully published');

We'll make a really similar class to this in the MVC object-oriented CMS in PHP course to display user messages. You'll make it there eventually, don't worry :)

Note: People used to declare constants using the define() function in outdated versions of PHP. However, the constants created using this function did not belong to any class, and completely did away with everything that OOP stands for.

In the next lesson, Static class members in PHP pt. 3 - do's and don'ts, we will keep working with static members and use them to make a static database wrapper and a static registry. We will also go over some more theory about good and bad ways of using static members. Today's lesson code files can be downloaded in the attachment below.



Downloaded 18x (3.64 kB)
Application includes source codes in language PHP



Article has been written for you by David Capka
Do you like this article?
No one has rated this quite yet, be the first one!
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn College The author learned IT at the Unicorn College - a prestigious college providing education on IT and economics.
Activities (9)




Justin Jake Telo:12. February 0:14

parent::__con­struct($firstNa­me, $lastName, $age, password);

why is the "password" have no $ sign? please enlighten me thank you very much

Reply 12. February 0:14
Justin Jake Telo:12. February 0:16

and why if (Human::valid­Password($pas­sword))

i look the variable for $password and it is private,,, i though variable static will do it the 'self:: thank you for answer

Reply 12. February 0:16
David Capka
ICT.social team
Replies to Justin Jake Telo
David Capka:12. February 5:41

I can't find any occurrence of , password) in this lesson, maybe you just forgot to write $?

In if (Human::validPassword($password)), $password is just a local variable which happens to have the same name as the $password property in the class. But these are 2 different variables, you would have to write $human->password to access the property. And as you correctly stated, it wouldn't work since it's private.

Edited 12. February 5:47
Reply 12. February 5:41
You can walk through a storm and feel the wind but you know you are not the wind.
Replies to David Capka
Justin Jake Telo:13. February 5:52

// Contents of the PhpProgrammer.php file
class PhpProgrammer extends Human

public $ide;

public function __construct($fir­stName, $lastName, $age, $password, $ide)
$this->ide = $ide;
parent::__con­struct($firstNa­me, $lastName, $age, password); // here from the phpProgrammer

Reply 13. February 5:52
David Capka
ICT.social team
Replies to Justin Jake Telo
David Capka:26. February 10:57

I've downloaded the attachment from this article and I can see the $ there. Can you please recheck it?

Edited 26. February 13:38
Reply 26. February 10:57
You can walk through a storm and feel the wind but you know you are not the wind.
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

5 messages from 5 displayed.