开发者

How to write an array to do all the setter things in php?

For example, I have a object like this:

class myObj{
 private $a;
 private $b;

 //getter , setter

}

And I would like to do something li开发者_JAVA百科ke:

$myObj = initWitharray(array('a'=> 'myavalue', 
                           'b'=> 'mybvalue'));

And the myObj will have all the a value and b value. How can I do so ? Thank you.


As NullUserException suggested:

<?php

class myObj {

    private $a;
    private $b;

    public function initWithArray(array $arr) {
        foreach ($arr as $k => $v) {
            $this->$k = $v;
        }
        return $this;
    }

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

}

// usage
$myObj = new myObj();
echo $myObj->initWithArray(array(
            'a' => 'myavalue',
            'b' => 'mybvalue'))
        ->get('a');


function initWithArray(array $a){
 $myObj = new myObj();

  foreach($a as $k => $v){
   $myObj->$k = $v;
 }

 return $myObj;
}

class myObj {
 private $a;
 private $b;

 public function __set($name, $value) {
   $this->$name = $value;
 }

 public function __get($name){
   if($this->$name != null)
     return $this->$name;

   return null;
 }
}

Or, as said in the comments, it's better if init function would be a member of a class.


Try the following:

class myObj {
    private $a;
    private $b;

    function __construct($passedArray){
        $this->a = array_key_exists('a', $passedArray) ? $passedArray['a'] : 'default_value_for_a';
        $this->b =  array_key_exists('b', $passedArray) ? $passedArray['b'] : 'default_value_for_b';
    }
//Rest of the code
}

Then:

newObj = new myObj(array('a'=> 'myavalue', 'b'=> 'mybvalue'))


You could use the class constructor to pass in options when you create a new object. Doing it this way, you should also separate out the setOptions method so you can update the options after init as well.

Use this class like this: (shows both ways to set options)

$object = new myClass(array('a'=>'foo'));
$object->setOptions(array('b'=>'bar'));

Also, try not to confuse object with class. An object is an instance of a class.

class myClass
{
    private $a;
    private $b;

    public function __construct(array $options = null)
    {
        if (null !== $options) {
            $this->setOptions($options);
        }
    }

    public function setOptions(array $options)
    {
        foreach ($options as $key => $value) {
            if (isset($this->$key)) {
                $this->$key = $value;
            }
        }

        return $this;
    }
}


I usually adopts the approach which gives me total control over the object, like allowing someone to access the property. denying the permission, allowing access to only those which i think is appropriate according to application etc. and that's the purpose of object.

Have a look at the example below.

Example

class MyObj {

    private $data = array('one' => null, 'two' => null);

    public function __set($property, $value) {
        //Only allow to set those properties which is declared in $this->data array
        if(array_key_exists($property, $this->data)) {
            return $this->data[$property] = $value;
        } else {
            //if you want to throw some error.
        }
    }

    //you can allow or disallow anyone from accessing the class property directly.
    public function __get($property) {
        //To deny the access permission, simply throw an error and return false.
        $error = 'access denied to class property {' . $property . '}';
        return false;
        //Or Else Allow permission to access class property
        //return $this->data[$property];
    }
}

the above example demonstrates on how you can gain more control over the class property, by declaring class property $data as private you are basically disallowing anyone to do any sort of manipulation on the class property directly. whatever operation is to be carried out is done through PHP's getter __get() and setter __set() method. of course you can modify the above code according to your need, you just new a very few line of changes and it will behave the way you want it to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜