The traditional development approach is to place the class into its own file. Typically, classes contain logic that implements a single purpose. Classes are further broken down into self-contained functions which are referred to as methods. Variables defined inside classes are referred to as properties. It is recommended to develop a test class at the same time, a topic discussed in more detail in Chapter 13, Best Practices, Testing, and Debugging.
class, add a DocBlock. You can then define properties and methods. In this example, we define a class Test. It has a property $test, and a method getTest():<?php
declare(strict_types=1);
/**
* This is a demonstration class.
*
* The purpose of this class is to get and set
* a protected property $test
*
*/
class Test
{
protected $test = 'TEST';
/**
* This method returns the current value of $test
*
* @return string $test
*/
public function getTest() : string
{
return $this->test;
}
/**
* This method sets the value of $test
*
* @param string $test
* @return Test $this
*/
public function setTest(string $test)
{
$this->test = $test;
return $this;
}
}Best practice
It is considered best practice to name the file after the class. Although class names in PHP are not case sensitive, it is further considered best practice to use an uppercase letter for the first name of a class. You should not put executable code in a class definition file.
Each class should contain a DocBlock before the keyword class. In the DocBlock you should include a short description of the purpose of the class. Skip a line, and then include a more detailed description. You can also include @ tags such as @author, @license and so on. Each method should likewise be preceded by a DocBlock that identifies the purpose of the method, as well as its incoming parameters and return value.
NameAddress.php, which defines two classes, Name and Address:<?php
declare(strict_types=1);
class Name
{
protected $name = '';
public function getName() : string
{
return $this->name;
}
public function setName(string $name)
{
$this->name = $name;
return $this;
}
}
class Address
{
protected $address = '';
public function getAddress() : string
{
return $this->address;
}
public function setAddress(string $address)
{
$this->address = $address;
return $this;
}
}TwoClass.php, we define two classes, TwoClass and twoclass:<?php
class TwoClass
{
public function showOne()
{
return 'ONE';
}
}
// a fatal error will occur when the second class definition is parsed
class twoclass
{
public function showTwo()
{
return 'TWO';
}
}$this. Although permitted in PHP 7.0 and PHP 5.x, any of the following uses of $this will now generate an error as of PHP 7.1, if $this is used as:static variableglobal variabletry...catch blocksforeach()unset()$a = 'this'; echo $$a)stdClass which is built into PHP. stdClass allows you to define properties on the fly without having to define a discreet class that extends stdClass:$obj = new stdClass();
PDO::FETCH_OBJ. This mode returns instances of stdClass where the properties represent database table columns:$stmt = $connection->pdo->query($sql); $row = $stmt->fetch(PDO::FETCH_OBJ);
Take the example for the Test class shown in the preceding code snippet, and place the code in a file named Test.php. Create another file called chap_04_oop_defining_class_test.php. Add the following code:
require __DIR__ . '/Test.php';
$test = new Test();
echo $test->getTest();
echo PHP_EOL;
$test->setTest('ABC');
echo $test->getTest();
echo PHP_EOL;The output will show the initial value of the $test property, followed by the new value modified by calling setTest():

The next example has you define two classes, Name and Address in a single file NameAddress.php. You can call and use these two classes with the following code:
require __DIR__ . '/NameAddress.php';
$name = new Name();
$name->setName('TEST');
$addr = new Address();
$addr->setAddress('123 Main Street');
echo $name->getName() . ' lives at ' . $addr->getAddress();The output from this example is shown next:

Step 3 also shows two class definitions in one file. In this case, however, the objective is to demonstrate that classnames in PHP are case-insensitive. Place the code into a file, TwoClass.php. When you try to include the file, an error is generated:

To demonstrate the direct use of stdClass, create an instance, assign a value to a property, and use var_dump()to display the results. To see how stdClass is used internally, use var_dump() to display the results of a PDO query where the fetch mode is set to FETCH_OBJ.
Enter the following code:
$obj = new stdClass(); $obj->test = 'TEST'; echo $obj->test; echo PHP_EOL; include (__DIR__ . '/../Application/Database/Connection.php'); $connection = new Application\Database\Connection( include __DIR__ . DB_CONFIG_FILE); $sql = 'SELECT * FROM iso_country_codes'; $stmt = $connection->pdo->query($sql); $row = $stmt->fetch(PDO::FETCH_OBJ); var_dump($row);
Here is the output:

For more information on refinements in PHP 7.1 on the keyword $this, please see https://wiki.php.net/rfc/this_var.