drewjoh Posted October 15, 2007 Share Posted October 15, 2007 I've been programming for a while... and it's been a long road to understanding and getting all the character sets correct for, well, everything. We have lots of international customers, and I quickly found that having MySQL = latin_1, HTML = 8859-1, and our credit processor = UTF-8... doesn't work very consistently. So going forward... I'm using MySQL = UTF-8_general, the MySQL connection = UTF-8_general... HTML = utf-8... and it's all working fine and good... until just now. This is what I'm finding.... This is my test string that I'm using: Bièr hjärt Roló 202 3º 1ª Creating a variable and echoing this in PHP works. Example: $var = 'Bièr hjärt Roló 202 3º 1ª'; echo $var; Creating a class with an empty variable, filling it, and echoing it works: Example: class Test { public $var; __construct($input) { $this->var = $input; } } $test = new Test('Bièr hjärt Roló 202 3º 1ª'); echo $test->var; However, creating a class with a pre-defined variable, and outputting it... doesn't. Example: class Test { public $var = 'Bièr hjärt Roló 202 3º 1ª'; __construct() { } } $test = new Test(); echo $test->var; I have searched high and low and can't figure this out for the life of me. Can anyone explain this? Or confirm/debunk my results? In the last example that doesn't work. I can say this: echo utf8_encode($test->var); And it works ok, but this doesn't explain why it does this, and is not really an acceptable solution. Seeing how pre-defined variables need to be encoded (shouldn't they be already?), but class variables set from outside don't. Thanks to anyone who can shed some light on this! Other information: using phpinfo();... - My "default_charset" in the php config is set to "no value". My character set for the HTML page is set to utf-8 and that's the type the page is using to render text. - Using a similar class, if I set a variable in the class and it then saves it to my database, and then retrieves it and outputs it... works correctly also. So it seems to only happen with a variable that's predefined in a class. Quote Link to comment https://forums.phpfreaks.com/topic/73277-character-set-inside-a-php-class/ Share on other sites More sharing options...
jd2007 Posted October 15, 2007 Share Posted October 15, 2007 add word function to __construct, like this: class Test { public $var = 'Bièr hjärt Roló 202 3º 1ª'; // here function __construct() { } } $test = new Test(); echo $test->var; it works fine ! Quote Link to comment https://forums.phpfreaks.com/topic/73277-character-set-inside-a-php-class/#findComment-369746 Share on other sites More sharing options...
drewjoh Posted October 15, 2007 Author Share Posted October 15, 2007 Thanks for your reply JD2007! When I saw your post I was like "oh, duh!" and went to do that, but... "function" was there, I just didn't include it when typing it out here in the forum. So... no go, ??? I must be doing something wrong... or something. Especially if this works for you... then I don't know what to think! I tried it some more, and this is what happens with two examples: - The first example of the test string is when saying "echo $test->var" when the variable was set in the class definition. - The second example of the test string is when setting the $var after initiating an instance of the class, and then outputting it. In Firefox, when I view source (Firefox source browser), I get: Bi?r hj?rt Rol? 202 3? 1? - Bièr hjärt Roló 202 3º 1ª And in IE, when I view source (in notepad), I get: Bièr hjärt Roló 202 3º 1ª - Bièr hjärt Roló 202 3º 1ª Firefox displays in the browser just what you see in source. IE displays "Bi�r hj�rt Rol� 202 3� 1� - Bièr hjärt Roló 202 3º 1ª" in the browser. I still don't get it! An hour or so more of searching and googling hasn't provided anything new. But at least I've figured out my limitations... Quote Link to comment https://forums.phpfreaks.com/topic/73277-character-set-inside-a-php-class/#findComment-370077 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.