Using string and functions together within php class properties

in my theme functions.php i created a class contains some properties. my problem this when i use both string and functions in my property class show me the error say syntax error, unexpected '.', expecting ',' or ';'

for example i want when i print this property in my class show me the post title within <h1><a href=""></a></h1> but when i use the the_title() wordpress function within html tag string show me the above error. how i can use the the_title() function until show me the title correctly?

class YPE_post_formats {
    public $VP_icon = '<h1><a href="">'.the_title().'</a></h1>';
}

Related posts

1 comment

  1. You cannot use functions in class property definition. Instead you could do it as follows:

    class YPE_post_formats {
        public $VP_icon;
    
        public function __construct($the_title)
        {
            $this->VP_icon = '<h1><a href="">'.$the_title.'</a></h1>';
        }
    }
    
    $obj = new YPE_post_formats(get_the_title());
    
    // To echo $VP_icon
    echo $obj->$VP_icon;
    

    Edit

    I edited my answer removing the definition of the_title() as it is already defined in WordPress. Furthermore, I changed the_title() to get_the_title() as this will not echo but get the contents of the title.

Comments are closed.