JohnnyFaldo
JohnnyFaldo

Reputation: 4161

Is this method a getter/setter or neither

If a method that takes no parameters, gets some data from a database, and then sets it as a property within the class.

Is that method considered a getter, setter, both, neither or is it doing too much?

Upvotes: 3

Views: 112

Answers (3)

Thibault
Thibault

Reputation: 1596

You current method is not a getter. Because it doesn't return data from the object.

And it contains a setter that you should extract as a real setData() method.

You should indeed split it in 2 methods:

function retrieveData() {
    // get data from the database
    return $data;
}

function setData($data);
    $this->data = $data;
}

function yourCurrentMethod() {
    $data = $this->retrieveData();
    $this->setData($data);
}

Upvotes: 1

Fluffeh
Fluffeh

Reputation: 33512

Technically, a getter and setter is something that is inside a class for when you either get or set a property (errrr variable) inside the class - when called from the code outside the class. These getter and setter functions are (in a good part) there to modify private properties of a class.

What you are describing is simply a function that sets a property, this is neither a setter or getter - it simply is.

An example of a getter would be a function that does something each and every time you get the value of property from it (like say incrementing a counter for how many times you got that property). A setter would be the opposite - in this case counting how many times you assigned a value to a property.

Really though, you should have a read of the in depth docs for both the set and get magic methods. Here is a quick sample of getter and setter classes pinched right off the PHP site:

<?php
class PropertyTest
{
    /**  Location for overloaded data.  */
    private $data = array();

    /**  Overloading not used on declared properties.  */
    public $declared = 1;

    /**  Overloading only used on this when accessed outside the class.  */
    private $hidden = 2;

    public function __set($name, $value)
    {
        echo "Setting '$name' to '$value'\n";
        $this->data[$name] = $value;
    }

    public function __get($name)
    {
        echo "Getting '$name'\n";
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }

        $trace = debug_backtrace();
        trigger_error(
            'Undefined property via __get(): ' . $name .
            ' in ' . $trace[0]['file'] .
            ' on line ' . $trace[0]['line'],
            E_USER_NOTICE);
        return null;
    }

    /**  As of PHP 5.1.0  */
    public function __isset($name)
    {
        echo "Is '$name' set?\n";
        return isset($this->data[$name]);
    }

    /**  As of PHP 5.1.0  */
    public function __unset($name)
    {
        echo "Unsetting '$name'\n";
        unset($this->data[$name]);
    }

    /**  Not a magic method, just here for example.  */
    public function getHidden()
    {
        return $this->hidden;
    }
}


echo "<pre>\n";

$obj = new PropertyTest;

$obj->a = 1;
echo $obj->a . "\n\n";

var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo "\n";

echo $obj->declared . "\n\n";

echo "Let's experiment with the private property named 'hidden':\n";
echo "Privates are visible inside the class, so __get() not used...\n";
echo $obj->getHidden() . "\n";
echo "Privates not visible outside of class, so __get() is used...\n";
echo $obj->hidden . "\n";
?>

Upvotes: 0

Terry Harvey
Terry Harvey

Reputation: 9444

That's not a getter. or a setter. A getter or setter is something that gets or sets a property inside a class.

<?php

class Person {
    protected $name;

    // Setter
    public function setName($name) {
        $this->name = $name;
    }

    // Getter
    public function getName() {
        return $this->name;
    }
}

What you're describing is an ORM (object-relational mapping) like Doctrine or Eloquent.

Upvotes: 0

Related Questions