Newbie: Problems with objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nicofast
Forum Newbie
Posts: 3
Joined: Fri Mar 24, 2006 1:47 pm

Newbie: Problems with objects

Post 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]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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());
Post Reply