PHP, MySQL, & JavaScript All-in-One For Dummies
Book image
Explore Book Buy On Amazon
Accessor magic methods are PHP methods you use to access the private property values you define in the class. Creating special methods to retrieve the current property values helps create a standard for how other programs use your class objects. These methods are often called getters because they retrieve (get) the value of the property.

You define the accessor using the special __get() method:

public function __get($name) {
     return $this->$name;
That's all there is to it! Accessor methods aren’t overly complicated; they just return the current value of the property. To use them you just reference the property name as normal:
echo "<p>Product: $prod1->description</p>\n";
PHP automatically looks for the accessor method to retrieve the property value. Follow these steps to try creating and using a class definition with mutators and accessors:
  1. Open your editor and type the following code:
    <!DOCTYPE html>
    <title>PHP OOP Test</title>
    <h1>Testing PHP OOP setters and getters</h1>
    class Product {
        private $description;
        private $price;
        private $inventory;
        private $onsale;

    public function __set($name, $value) { if ($name == "price" && $value < 0) { echo "<p>Invalid price set<p>\n"; $this->price = 0; } elseif ($name == "inventory" && $value < 0) { echo "<p>Invalid inventory set: $value</p>\n"; } else { $this->$name = $value; } }

    public function __get($name) { return $this->$name; }

    public function buyProduct($amount) { if ($this->inventory >= $amount) { $this->inventory -= $amount; } else { echo "<p>Sorry, invalid inventory requested: $amount</p>\n"; echo "<p>There are only $this->inventory left</p>\n"; } } }

    $prod1 = new Product(); $prod1->description = "Carrots"; $prod1->price = 1.50; $prod1->inventory = 5; $prod1->onsale = false;

    echo "<p>Just added $prod1->inventory $prod1->description</p>\n";

    echo "<p>Now buying 4 carrots...<p>\n"; $prod1->buyProduct(4); echo "<p>Inventory of $prod1->description is now $prod1->inventory</p>\n";

    echo "<p>Trying to set carrot inventory to -1:</p>\n"; $prod1->inventory = -1;

    echo "<p>Now trying to buy 10 carrots...</p>\n"; $prod1->buyProduct(10); echo "<p>Inventory of $prod1->description is now $prod1->inventory</p>\n"; ?> </body> </html>

  2. Save the file as ooptest2.php in the DocumentRoot folder for your web server.
  3. Ensure that the web server is running, and then go here to test your code.
  4. Close the browser window when you're done.
The image below shows the output that you should see when you run the program in your browser.

ooptest2.php The output from the ooptest2.php program.

There’s a lot going on in this example, so hang in there with me! First, the PHP code defines the Product class, using the four properties, but this time it defines them with private visibility. Following that, the mutator and accessor magic methods are defined. The mutator checks to ensure the price and inventory properties can't be set to a negative value.

After the class definition, the code creates an instance of the Product class, and experiments with the inventory values. First, it uses the buyProduct() method to purchase four carrots. That works just fine.

Next, it uses the mutator to set the inventory property for the carrot object to a negative value. The mutator code intercepts that request and prevents the inventory from being set, instead producing an error message.

Finally, the code tries to use the buyProduct() method to purchase more carrots than what's set in inventory. The added code in the buyProduct() method prevents that from happening.

Now the class definition is starting to do some useful functions for the application. But wait, there are more magic methods available for you to use!

About This Article

This article is from the book:

About the book author:

Richard Blum has more than 30 years of experience as a systems administrator and programmer. He teaches online courses in PHP, JavaScript, HTML5, and CSS3 programming, and authored the latest edition of Linux For Dummies.

This article can be found in the category: