Problem with a php class __construct

posted by web10 on 2009-06-22 00:00:00
Hello everybody!
It's my first message, thank you in advance for your help.
I have this simple class:
 
class Simple{ 
	//Variables declaration
	var $it = '';
	var $en = '';
	var $fr = '';
 
	function __construct($it, $en, $fr){
		$this->it = $it;
		$this->en = $en;
		$this->fr = $fr;
	}
 
	function get_desc(){
		return $this->it; 
		return $this->en;
		return $this->fr;
	}	
}
 
I get a warning when I try to inizialize a new object of this class.
 
$en  = new Simple($_POST['en']);
Warning: Missing argument 2 for Simple::__construct()
 
What could i do?
Thank you!!
dail avatar
dail
12
Hi web10,
There are three "return" in get_desc() function, it's wrong. it's impossible to return three values.
You should do:
 
class Simple{ 
	var $descr = array();
 
	function __construct($descr_list){
		$this->descr = $descr_list;
	}
 
	function set_desc($lng, $s){
		$this->descr[$lng] = $s;
	}
 
	function get_desc($lng){
		if (isset($this->descr[$lng]))
			return $this->descr[$lng];
	}	
}
 
$a = array(
	'it'=>'it descr',
	'en'=>'en descr',
	'fr'=>'fr descr'
);
 
$obj = new Simple($a);
echo $obj->get_desc('it').'<br />';
 
$obj->set_desc('it', 'new description');
 
echo $obj->get_desc('it').'<br />';
 
I hope this code will help you.
Bye
49 answers - 0 questions
+ 1  Positive        Negative

web10
0
Thank you dail, I will vote you :-)
How to print all the descriptions?
 
	function get_desc($lng){
		echo $this->get_desc('it');
		echo $this->get_desc('en');
		echo $this->get_desc('fr');
	}
 
Is it correct?
5 answers - 8 questions
  Positive        Negative

dail avatar
dail
12
Posted by web10:
Thank you dail, I will vote you :-)
How to print all the descriptions?
 
	function get_desc($lng){
		echo $this->get_desc('it');
		echo $this->get_desc('en');
		echo $this->get_desc('fr');
	}
 
Is it correct?

I don't like it so much. You should do something like that:
 
	function print_all_descr(){
		foreach ($this->descr AS $key => $value){
			echo "$key => $value<br/>";
		}
	}
 
$obj->print_all_descr();
 
You are welcome.
49 answers - 0 questions
  Positive        Negative



Answer the question



Top Users
  • dail (12)
  • livin52 (3)
  • camu (3)
  • softweb (2)
  • Nadine (1)
  • Josware (1)
  • lfelipecr (1)
  • gregoriohc (1)
  • Mitu (1)
  • ryan714 (1)