__get() __set()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
manus
Forum Newbie
Posts: 2
Joined: Fri Mar 27, 2009 6:01 am

__get() __set()

Post by manus »

Hi, I'm learning PHP and have ran into some confusion with the textbook I'm using, which had no errata support.

Executing the following example, from my textbook, seems to bypass the __set and __get accessor functions:

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

function __set ($name, $value)
{
if(($name == "attribute") && ($value >= 0) && ($value <= 100)){
$this->attribute = $value;
}
}
}

$a = new classname();

$a->attribute = 500;

echo $a->attribute;

But when I change the access modifier to "private" it seems to work ok, and use the.

Can someone tell me what it is supposed to be?

Many thanks.
JasonDFR
Forum Commoner
Posts: 40
Joined: Wed Jan 07, 2009 3:51 am

Re: __get() __set()

Post by JasonDFR »

Code: Select all

<?php
 
class classname
{
    private $attribute;
 
    function __get($name)
    {
        return $this->$name;
    }
 
    function __set ($name, $value)
    {
        if(($name == "attribute") && ($value >= 0) && ($value <= 100)){
            $this->attribute = $value;
        } else {
            throw new Exception("Object does not allow variable: $name to be set with value: $value");
        }
    }
}
 
$a = new classname();
 
try {
    $a->attribute = 99;
    //$a->attribute = 500;
    echo $a->attribute;
} catch (Exception $e) {
    print_r($e);
}
The code you posted works just like it would be expected to, however it really doesn't make sense to write a __set() function that places constraints on a specific variable and then set that variable to public. When $attribute is public, and you execute :$a->attribute = 500; __set() is bypassed because the variable attribute can be accessed without the need of a setter function or a magic __set() function. When you make $attribute protected or private a setter function is necessary because it can only be accessed from within the class. In this case, __set() is used because there is no setAttribute() function to specificlly handle changing the value of $attribute. When a variable is public, it can be changed from anywhere without the aide of __set() or setAttribute().

Generally speaking, all variables should be protected or private as this is really at the heart of the encapsulation theory of OOP. When you use a variable that needs to be manipulated, make sure to code a setVariable() function. Likewise when you have a varaible that needs to be echoed or used outiside of its class or in another class, code a getVariable() function.

__set and __get are not functions that should be included in every class.

If I didn't answer well enough, let me know what is still confusing.
manus
Forum Newbie
Posts: 2
Joined: Fri Mar 27, 2009 6:01 am

Re: __get() __set()

Post by manus »

Many, many thanks for the clariffication.

This is what I had thought, but, as I'm only learning, I presumed the book I am studying from, "PHP and MySQL Web Development" by Luke Welling and Laura Thomson, would be authoritative. As it turns out, the book is full of errors and misguided information, leaving one totally discouraged.

Many thanks again.
JasonDFR
Forum Commoner
Posts: 40
Joined: Wed Jan 07, 2009 3:51 am

Re: __get() __set()

Post by JasonDFR »

No problem.

One of the best beginner's books is "PHP A Beginner’s Guide" by Vikram Vaswani.
Post Reply