2

I'm starting to work with classes in PHP.

I have been reading and I noticed PHP is all about arrays.

So I was wondering if it would be a good practice to use the class properties inside array and naming them after keys.

Like this:

private $prefix;
private $name;
public function setPrefix($p)
{
    $this->prefix = $p;
}
public function getPrefix()
{
    return $this->prefix;
}
public function setName($n)
{
    $this->name = $n;
}
public function getName()
{
    return $this->name;
}

That's the common way of doing this.

But instead do it like this:

private $data = array();

public function setData($property, $value)
{
    $this->data[$property] = $value;
}
public function getData($property)
{
    return $this->data[$property];
}

Would this be better than the common way? I believe that would be a generic class structure for any database table.

1
  • 2
    Depends on the needs of your application. What you suggest here is somehow close to how EAV model works which have pros and cons.(devdocs.magento.com/guides/m1x/magefordev/mage-for-dev-7.html - magento tutorial but explains very well what eav model is in "What is EAV" section) Commented Oct 28, 2015 at 11:45

2 Answers 2

4

Would this be better than the common way?

NO. And in fact it have drawbacks.

  • It removes the public, protected and private encapsulation of your properties (which is in the essence of oop).
  • Adds a layer over every variable access. I don't really know the internals of php, but I really don't think it could be faster than native properties. (although the difference is probably absolutely irrelevant to any script)
  • IDE's won't be able to complete your code when accessing properties.

It can have it's uses, if your class is a container which needs to have an array of internal data, in which case you would class container implements ArrayAccess and use it like an array, instead of global get/set methods. Here the documentation for ArrayAccess()

$obj = new container();
$obj['key'] = "value";
echo $obj['key'];

Bottom line

Why try and reinvent the wheel? A property is a property. There is no logical or semantical improvement in wrapping every property inside another property. It's obsfucating everything. It won't be faster, it won't be clearer, it removes the oop concepts from your properties and it's just going against the current of using objects in the first place.

About easier database management

If you really want to easily pass an array to a prepared statement, you can get the properties of an object with get_object_vars($obj), no need to put them in an array before for this very purpose. Moreover, as noted by Cypher, you won't be able to use the built-in fetchObject() method, which completely nullify the time you will not have gained by having an easier time querying the database.

Sign up to request clarification or add additional context in comments.

3 Comments

well I didn't lose the private property, you can see I made a getter and setter
Yes you did. They are either all private or all public or all protected.
But all the properties should be private, being accessible only via getter/setters, right?
0

This will make it easy to automate DB Operations.

But will make it hard for to use the object by humans.

Yii(2) uses this setup as part of there ActiveRecords but extend it by defining the properties as a comment

/**
 * @property int $id
 * @property string $name
 */
class SomeClass extends AbstractModel 

And also implements magic methods: __get(), __set()` so you can easily set and get properties like this:

class AbstractModel{

public function __get($name){ 
    if(isset($this->data[$name])){
        return $this->data[$name]; 
    }else{
        throw new Exception("Undefined or property '$name'");
    }
}

public function __set($name, $value){
    if(isset($this->data[$name])){
        return $this->data[$name] = $value;
    }else{
        throw new Exception("Undefined or property '$name'");
    }
}

4 Comments

"This will make it easy to automate DB Operations." sorry to ask but how would that be?
By getting the sql row data and populating the "properites" as associative array. This will have several drawbacks ... like validation, not matching names etc. which can be addressed later when you understand them :)
exactly like get_object_vars()? :) (my point was how would that be easier)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.