There is a code

class emp { public function __construct($sur, $nam, $pat, $age = 30) { $this->sur = $sur; $this->nam = $nam; $this->pat = $pat; $this->age = $age; } public function __toString() { return "{$this->sur} {$this->nam[0]}. {$this->pat[0]}."; } public function __get($index) { return $this->$index; } public $sur; public $nam; private $pat; } 

Why is the first variable displayed normally, and the initials of the variables nam, pat displayed with question marks?

    2 answers 2

    The lines $nam and $pat are probably represented in Unicode. It can contain several bytes per character. The $nam[0] construct does not pull out the entire character, but only its first byte, hence the problem.

    Try to make a normal conclusion:

    public function toString() { return "{".$this->sur."} {".$this->nam[0]."}. {".$this->pat[0]."}."; }

    • does not roll, I even got an error when I changed __toString to toString - Roman Sokolov
    • @ t0di, the display of brackets, the syntax "{$ this-> sur} {$ this-> nam [0]}. {$ this-> pat [0]}." used to not write $ this-> sur. "". $ this-> nam [0]. ".". $ this-> pat [0]. "." Those. in order to display the property of an object in a string, it is sufficient to limit them to curly strings. @ Roman Sokolov, I don’t remember exactly, but it seems like for these purposes you should use substr , not $ this-> nam [0] - BOPOH