Where and why do we use __toString() in PHP?

后端 未结 10 1651
星月不相逢
星月不相逢 2021-01-01 10:34

I understand how it works but why would we practically use this?



        
10条回答
  •  醉梦人生
    2021-01-01 11:11

    it's like override with c# :

    class Person
    
    {
    
    public Person(string firstName, string lastName)
    
    {
    
    FirstName = firstName;
    
    LastName = lastName;
    
    }
    
    public string FirstName { get; set; }
    
    public string LastName { get; set; }
    
    public override string ToString()
    
    {
    
    return FirstName + “ “ + LastName;
    
    }
    
    }
    

提交回复
热议问题