I have tried the solutions indicated here on stackoverflow, the code below uses one of them, recommended and voted as the right way to do it, but it doesn't work for me, why? In fact the href results empty.
<?php
//URLS LIST
$nameA = 'http://www.example.com';
$nameB = 'http://www.example.com';
$nameC = 'http://www.example.com';
class bannClass {
private $class_varA;
private $class_varB;
private $class_varC;
public $username = '';
public function __construct($nameA, $nameB, $nameC) {
$this->class_varA = $nameA;
$this->class_varB = $nameB;
$this->class_varC = $nameC;
}
public function check_userOne() {
$url = 'https://example.com/wp-content/uploads/sponsor/' . $this->username . '/sponsor1.jpg';
return '<a href="' . $this->class_varA . '" target="_blank" rel="noopener noreferrer"><img src="' . $url . '" alt="Sponsor"/></a>';
}
public function check_userTwo() {
$url = 'https://example.com/wp-content/uploads/sponsor/' . $this->username . '/sponsor2.jpg';
return '<a href="' . $this->class_varB . '" target="_blank" rel="noopener noreferrer"><img src="' . $url . '" alt="Sponsor"/></a>';
}
public function check_userThree() {
$url = 'https://example.com/wp-content/uploads/sponsor/' . $this->username . '/sponsor3.jpg';
return '<a href="' . $this->class_varC . '" target="_blank" rel="noopener noreferrer"><img src="' . $url . '" alt="Sponsor"/></a>';
}
}
Also how can i make those 3 variables at the top dynamic in php? instead of "name" something like $($this->username . 'A')
, $($this->username . 'B')
, etc.
EDIT: the above class is being instantiated in another php file like so:
<?php
require_once('myclass.php');
$bannClass = new bannClass();
$bannClass->username = $data['username'];
//etc.
and used like:
<?php echo $bannClass->check_userOne();?>