Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6044643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:02:06+00:00 2026-05-23T07:02:06+00:00

This might look as a stupid question. But, I have a class with some

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:02:06+00:00Added an answer on May 23, 2026 at 7:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might look like a basic question to some of you but I expect
This might seem like a stupid question I admit. But I'm in a small
This might be a stupid question, but I notice that in a good number
This might look like a beaten question, but could not find an answer at
Well this question might look simillar to questions easily findable with google but for
This might be a stupid question but if there's a better or proper way
This might look a repetition to my earlier question. But I think its not.
Now this might look like a duplicate thread, but my question is that I
All this might look too trivial but read it through - I have simple
this might be a stupid question, but I'm using asp.net list control that creates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.