PHP operating on a variable upon setting it in PHP - php

This might look as a stupid question. But, I have a class with some public string variables defined in it.
Upon assigning a value to a property:
$a = new user();
$a->FirstName = "sth";
I want to store the value as UTF8.
I know I can do this via:
$a->Firstname = utf8_encode("sth");
However, I want the object to do this automatically.
How can I do this?

Otherwise no, the object cannot do it automatically.
Not automatically, but automagically!
<?php
class User {
/**
* Change the public to private/protected!
*/
private $Firstname;
/**
* This is automatically called upon calling a value that can't be written "from the outside".
*/
public function __set( $key, $value ) {
$this->$key = utf8_encode( $value );
}
public function __get( $key ) {
return isset( $this->$key ) ? $this->$key : false;
}
}
$user = new User;
$user->Firstname = 'Berry';
echo $user->Firstname;
The better solution would be to refactor in using mutators and accessors, or better yet, learn OO.

You want to use setters and getters. See: http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29
Like:
class User
{
protected $Firstname;
public function setFirstname($Firstname) {
$this->Firstname = utf8_encode($Firstname);
}
public function getFirstname() {
return $this->Firstname;
}
}
Example using magic methods:
class User
{
protected $data = array(
'Firstname' => '',
// ...
);
public function __set($key, $value) {
if (isset($this->data[$key])) {
$this->data[$key] = utf8_encode($value);
}
}
public function __get($key) {
return isset($this->data[$key]) ? $this->data[$key] : null;
}
}
Edit: I'm using $data so that there is at least a minimum of control of what properties can be set.

If you'd designed your class to have accessors and mutators, rather than public access to raw variables, then this would be easy.
Original code:
class user {
private $FirstName = '';
public function getFirstName() {
return $this->FirstName;
}
}
Solution code:
class user {
private $FirstName = '';
public function getFirstName() {
return utf8_encode($this->FirstName);
}
}
I suggest moving towards this approach.
Otherwise no, the object cannot do it automatically.
Edit
__set and __get might be the most appropriate way to implement this. I'm not too familiar with them, and it doesn't really matter: the point I'm making here is to use accessors and mutators... however you end up implementing them.

Related

How do i deal with multiple contructor arguments or class variables?

How do I know what to load in a constructor and what to set using the set methods later on?
For example, I have a question class which most of the time will call the following vars:
protected $question;
protected $content;
protected $creator;
protected $date_added;
protected $id;
protected $category;
At the moment I have it so only the bare essentials $id, $question, and $content are set in the constructor so I don't start building up a huge list of constructor arguments. This however, means that when I make a new question object elsewhere, I have to set the other properties of that object straight after meaning 'setter code' getting duplicated all over the place.
Should I just pass them all into the constructor right away, do it the way I'm doing it already, or is there a better solution that I'm missing? Thanks.
Depending on the language you can have multiple constructors for any one class.
You could use an array as the parameter to the constructor or setter method.
Just example:
public function __construct($attributes = array()) {
$this->setAttributes($attributes);
}
public function setAttributes($attributes = array()) {
foreach ($attributes as $key => $value) {
$this->{$key} = $value;
}
}
PHP doesn't support traditional constructor overloading (as other OO languages do). An option is to pass an array of arguments into the constructor:
public function __construct($params)
{
}
// calling it
$arr = array(
'question' => 'some question',
'content' => ' some content'
...
);
$q = new Question($arr);
Using that, you're free to pass a variable number of arguments and there is no dependency on the order of arguments. Also within the constructor, you can set defaults so if a variable is not present, use the default instead.
I would pass an array to the constructor with the values I want to set.
public function __construct(array $values = null)
{
if (is_array($values)) {
$this->setValues($values);
}
}
Then you need a method setValues to dynamicly set the values.
public function setValues(array $values)
{
$methods = get_class_methods($this);
foreach ($values as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
return $this;
}
For this to work you need setter methods for your properties like setQuestion($value) etc.
A fluent interface is another solution.
class Foo {
protected $question;
protected $content;
protected $creator;
...
public function setQuestion($value) {
$this->question = $value;
return $this;
}
public function setContent($value) {
$this->content = $value;
return $this;
}
public function setCreator($value) {
$this->creator = $value;
return $this;
}
...
}
$bar = new Foo();
$bar
->setQuestion('something')
->setContent('something else')
->setCreator('someone');
Or use inheritance...
class Foo {
protected $stuff;
public function __construct($stuff) {
$this->stuff = $stuff;
}
...
}
class bar extends Foo {
protected $moreStuff;
public function __construct($stuff, $moreStuff) {
parent::__construct($stuff);
$this->moreStuff = $moreStuff;
}
...
}
Or use optional parameters...
class Foo {
protected $stuff;
protected $moreStuff;
public function __construct($stuff, $moreStuff = null) {
$this->stuff = $stuff;
$this->moreStuff = $moreStuff;
}
...
}
In any case, there are many good solutions. Please dont use a single array as params or func_get_args or _get/_set/__call magic, unless you have a really good reason to do so, and have exhausted all other options.

PHP 5 how to call multiple values from one function?

If I have the following class example:
<?php
class Person
{
private $prefix;
private $givenName;
private $familyName;
private $suffix;
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}
public function getPrefix()
{
return $this->prefix;
}
public function setGivenName($gn)
{
$this->givenName = $gn;
}
public function getGivenName()
{
return $this->givenName;
}
public function setFamilyName($fn)
{
$this->familyName = $fn;
}
public function getFamilyName()
{
return $this->familyName;
}
public function setSuffix($suffix)
{
$this->suffix = $suffix;
}
public function getSuffix()
{
return $suffix;
}
}
$person = new Person();
$person->setPrefix("Mr.");
$person->setGivenName("John");
echo($person->getPrefix());
echo($person->getGivenName());
?>
I there a way in PHP (5.4 preferably), to combine these return values into one function, this way it models a little bit more like the revealing module pattern in JavaScript?
UPDATE:
OK, I am now beginning to learn that within PHP, it is normative to return a single value from a function, but you "can" return an array of multiple values. This is the ultimate answer to my question and what I will dive into some practices with this understanding.
small example -
function fruit () {
return [
'a' => 'apple',
'b' => 'banana'
];
}
echo fruit()['b'];
Also an article I ran across on stackoverflow on the topic...
PHP: Is it possible to return multiple values from a function?
Good luck!
You sound like you want the __get() magic method.
class Thing {
private $property;
public function __get($name) {
if( isset( $this->$name ) {
return $this->$name;
} else {
throw new Exception('Cannot __get() class property: ' . $name);
}
}
} // -- end class Thing --
$athing = new Thing();
$prop = $athing->property;
In the case that you want all of the values returned at once, as in Marc B's example, I'd simplify the class design for it thusly:
class Thing {
private $properties = array();
public function getAll() {
return $properties;
}
public function __get($name) {
if( isset( $this->properties[$name] ) {
return $this->properties[$name];
} else {
throw new Exception('Cannot __get() class property: ' . $name);
}
}
} // -- end class Thing --
$athing = new Thing();
$prop = $athing->property;
$props = $athing-> getAll();
Perhaps
public function getAll() {
return(array('prefix' => $this->prefix, 'givenName' => $this->giveName, etc...));
}

PHP static class return reference

I have class with a static method. The static method returns a private static stdClass object.
myclass::get() // returns stdClass object
myclass::get()->name // name is hardcoded into the class
How would I change name's value like:
myclass::get()->name = 'bob';
and have it set?
I tried returning the object like:
return &self::$static_object;
But that throws syntax errors.
What can i do?
EDIT posted code for clarification
final class config {
private static $configs = array();
public static function get($config_name) {
if (isset($configs[$config_name])) {
return self::$configs[$config_name];
}
$file = __get_file_exists(M_CONFIGS . $config_name, 'conf.');
if ($file) {
$config = self::__scope_include($file);
if (!is_array($config) && !$config instanceof stdClass) {
/*
*
*
* FIX
*
*
*
*/
die('ERROR config.php');
}
return self::$configs[$config_name] = self::__to_object($config);
}
}
private static function __scope_include($file) {
return include $file;
}
private static function __to_object($config) {
$config = (object) $config;
foreach ($config as &$value) {
if (is_array($value)) {
$value = self::__to_object($value);
}
}
return $config;
}
}
echo config::get('people')->name; //dave
config::get('people')->name = 'bob';
echo config::get('people')->name; // should be bob, is dave
Returning by reference in the get() method should do the trick:
public static function &get() {
return self::$static_object;
}
But, I think you should revisit your design, as this kind of coding is highly frowned upon and will cause maintenance and testability headaches down the road.
You missed self in if (isset($configs[$config_name])) {. It should be
if (isset(self::$configs[$config_name])) {
return self::$configs[$config_name];
}
Otherwise each time you call config::get('people'), you will be reading your config file which most likely returns an array and convert it to an object before returning it. Any changes you make to the object in self::$configs[$config_name] are overwritten by the newly created object.
What you are doing and the answer from drrcknlsn break Encapsulation. That is bad.
The correct way to do this is to create a setter method.
public static function set($key, $value) {
// set $config property...
}

Class function only available to "parent" - PHP

I want to create a function in a class that is available for a set of users, but that they won't be able to access. Ex:
class Stuff_for_user {
private $errors;
/*
* private $errors gets modified by private functions
*/
public function get_errors(){ // This is for users to display errors.
return $this->errors;
}
/*something here...*/ function set_errors($str){
$this->errors = $str;
}
}
So far so good, but now I want the parent class to be able to set Stuff_for_User's errors:
class Main_mess {
public index(){
$user_available_data = new Stuff_for_user();
if($big_error)
$user_available_data->set_errors("BIG ERROR!!!");
$this->send_to_users($user_available_data);
}
}
I want only Main_mess to be able to access Stuff_for_User's set_errors() method. Is that possible?
No, that is not possible like that, since Main_mess is not a parent class of Stuff_for_users (and this is probably what you want, looking at what your code actually does). So set_errors has to be public if you want to call it from the outside.
This is not possible how you want to implement it.
Some ideas (i dont know why or how you want to do that but just ideas...):
do set_error($str,$access_key) and let $access_key be an access string only you know!
let Stuff_for_user be in Extended_Stuff_for_user which has the set_error function like:
class Extended_Stuff_for_user {
private $errors;
private $Stuff_for_user;
public function set_errors() {
/* ... */
}
public function getStuffForUser() {
return $this->Stuff_for_user;
}
}
It seems that you are looking for implementation of something called friend class in php. Well .. i'm sorry to tell you this, but it is not possible.
You should look at other possible solutions to your problem.
class SecureContainer{
protected $user = null;
protected $target = null;
public function __construct( $target, $user )
{
$this->target = $target;
$this->user = $user;
}
public function __call( $method, $arguments )
{
if ( $this->user->isAllowed(getType( $this->target ), $method))
{
return call_user_func_array(
array( $this->target, $method), $arguments );
}
}
}
Use it like this:
$something = new UnsecureSomething;
$user = new User( $uid );
$something = new SecureContainer( $something, $user );
This should let you control the access to methods.
Yes it possible but it can be dirty.
Like This.
class Stuff_for_user {
private $errors;
/*
* private $errors gets modified by private functions
*/
public function get_errors(){ // This is for users to display errors.
return $this->errors;
}
/*
This way the child classes of Main will able be to use the set_errors function;
*/
function set_errors($class,$str){
if($class instanceof Main_mess)
{
$this->errors = $str;
}
/*
AndThis way the only Main_mess will be able;
*/
function set_errors($class,$str){
if(get_class($class)=="Main_mess")
{
$this->errors = $str;
}
}
class Main_mess {
public index(){
$user_available_data = new Stuff_for_user();
if($big_error)
$user_available_data->set_errors($this,"BIG ERROR!!!");
$this->send_to_users($user_available_data);
}
}

How will i pass an array as key to __set magic method

I have a php singleton session class as follows.
class Session {
static private $_instance = NULL;
private function __construct()
{
session_start();
}
/**
* Prevents the class from being cloned
* #return NULL
*/
private function __clone() { }
/**
* Returns the singleton instance of this class
* #return Session
*/
public static function getInstance()
{
if (!self::$_instance) {
self::$_instance = new Session();
}
return self::$_instance;
}
public function __get($key) {
if (isset($_SESSION[$key])) {
return $_SESSION[$key];
}
return NULL;
}
public function __set($key, $value)
{
$_SESSION[$key] = $value;
}
public function __isset($key) {
return isset($_SESSION[$key]);
}
public function __unset($key) {
unset($_SESSION[$key]);
}
}
I can create an object as follows
$session = Session::getInstance();
$session->name = 'some name';
I can also get the value like
echo $session->name;
The problem is, i want to pass an array to this object and it is not working. for example, i wan to set something like
$_SESSION['user']['data'] = array('name'=>'some name',"empId"=>'123');
I am trying like this.
$session->['user']['data'] = array('name'=>'some name',"empId"=>'123');
but it is not working. Could you please suggest what is wrong.
The workaround in this case would be to use:
public function &__get($key) {
if (isset($_SESSION[$key])) {
return & $_SESSION[$key];
}
return NULL;
}
You need to modify the __get() method, because an assignment like
$session->user['data'] = ...
will actually retrieve the [user] key, and then try to assign a new subarray [data] to that temporary array result.
Also note that $session->['user']['data'] is invalid syntax. You either need $session->user['data'] or $session->{'user'}['data'].
Anyway, I think it is probably not a good idea to use a wrapper if you often want to do assignments like that. (I do actually have something very similar.)
$session->user = array('data' => array('name'=>'some name',"empId"=>'123'));
Make sure you don't overwrite anything else in user you want to keep

Categories