Page 1 of 1

Newbie: Problems with objects

Posted: Thu Sep 21, 2006 4:31 am
by Nicofast
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, I have a simple code

Code: Select all

class Mes{
	var $dias=0;
	var $nombre;
	function Mes($nombre,$dias) {
		$this->nombre=$nombre;
		$this->dias=$dias;
	}
	function dias() {return $this->dias;}
	function nombre() {return $this->nombre;}
}

class Anio {

 	var $meses;
	static $diasSemana=Array("L","M","X","J","V","S","D");
 	
 	function Anio() {
 	 	$meses=Array(
 			new Mes("Enero",31),
 			new Mes("Febrero",28),
 			new Mes("Marzo",31),
 			new Mes("Abril",30),
 			new Mes("Mayo",31),
 			new Mes("Junio",30),
 			new Mes("Julio",31),
 			new Mes("Agosto",31),
 			new Mes("Septiembre",30),
 			new Mes("Octubre",31),
 			new Mes("Noviembre",30),
 			new Mes("Diciembre",31)
 		);
	}
	function mes($n) {return $this->meses[$n];}
	function diasem($n) {return $this->diasSemana[$n];}
}

$a=new Anio();
echo($a->mes[0]->nombre());
I'm trying to use the methods of the object type "Mes" included in the Array, but I can't.

PHP Fatal error: Call to a member function nombre() on a non-object in .... (in the final echo).

Can anyone tell me how can I make this? I'm using PHP5.

Thank you!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Sep 21, 2006 4:42 am
by volka

Code: Select all

class Mes{
   var $dias=0;
   var $nombre;
   function Mes($nombre,$dias) {
      $this->nombre=$nombre;
      $this->dias=$dias;
   }
   function dias() {return $this->dias;}
   function nombre() {return $this->nombre;}
}

class Anio {

    var $meses;
   static $diasSemana=Array("L","M","X","J","V","S","D");
    
    function Anio() {
        $this->meses=Array(
          new Mes("Enero",31),
          new Mes("Febrero",28),
          new Mes("Marzo",31),
          new Mes("Abril",30),
          new Mes("Mayo",31),
          new Mes("Junio",30),
          new Mes("Julio",31),
          new Mes("Agosto",31),
          new Mes("Septiembre",30),
          new Mes("Octubre",31),
          new Mes("Noviembre",30),
          new Mes("Diciembre",31)
       );
   }
   function mes($n) {return $this->meses[$n];}
   function diasem($n) {return $this->diasSemana[$n];}
}

$a=new Anio();
echo($a->mes(0)->nombre());
changed
$meses=Array(
to

Code: Select all

$this->meses=Array(
and
echo($a->mes[0]->nombre());
to

Code: Select all

echo($a->mes(0)->nombre());