1

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?

2
  • Maybe I'm not getting this straight, but you're hardcoding this string, right? Then just saving your .php file in UTF8 would suffice. Commented Jun 17, 2011 at 10:10
  • Are you sure you know what utf8_encode does? It very specifically converts ISO-8859-1 strings to UTF-8. How can you guarantee that all strings you want to store in this object will be ISO-8859-1 encoded? Commented Jun 17, 2011 at 10:50

3 Answers 3

2

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.

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

5 Comments

by the way, can u elaborate how can I use this? it's like I say: $a->FirstName = "sth"; ??
@Eskach While this will work short-term, you might want to consider refactoring to a more encapsulated version. This will save your behind in the long run.
@Eskach Updated this with a small usage example. It works by changing the public class members to private, and __set( ) will be called upon calling $user->Firstname.
And, would u please tell me what will happen when a function inside the class tries to assign a value to the property? Is the __set also called?
@Eskach No, __set will only be called if the variable is private and unwriteable. Private variables are writeable from within the class itself, meaning it would be written to directly, as opposed to being proxied through __set.
1

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.

3 Comments

so, this is not possible to do with __set() ?? I read everywhere that these are for undefined variables only.
@Eskach: __set is fine too. Probably preferable actually (though I'm not familiar with it). The point here is to use accessors and mutators, however you go about doing that.
@Eskach See the 2nd example. Hope it helps. Also I really don't like this approach. Neither public properties nor magic methods for getting and setting properties. But that's just my opinion.
0

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.

Comments

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.