Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: Grave en 17 Junio 2014, 10:59 am



Título: Duda sobre variables
Publicado por: Grave en 17 Junio 2014, 10:59 am
Alguien sabe porque este codigo me da error?
Código:
<?php
 
//no direct access
defined('_JEXEC') or die('Acceso restringido');

class Organismo
{
  private $idorganismo=0;
  private $nombre="";
  private $pais="";
  private $descripcion="";
 
  public function __construct() {
    $idorganismo=$_POST['codigo'];
    $nombre=$_POST['nombre'];
    $pais=$_POST['continente'];
    $descripcion=$_POST['informacion'];
    $db = JFactory::getDBO();   
  }
  public function crear(){
    $query = "INSERT INTO organismos VALUES(" . $idorganismo . ", '" . $nombre . "', '" . $pais . "', '" . $descripcion . "');";
    $db->setQuery($query);
    return $db->query();
  }
  public function editar(){
    echo $nombre;
    $query = "UPDATE organismos SET nombre='" . $nombre . "', continente='" . $pais . "', informacion='" . $descripcion . "' WHERE codigo=". $idorganismo . ";";
    $db->setQuery($query);
    return $db->query();
  }
  public function eliminar(){
    $query = "DELETE FROM organismos WHERE codigo='" . $idorganismo . "';";
    $db->setQuery($query);
    return $db->query();
  }
}
$input = new JInput;
$event = $input->get('event', '','string');
$res=false;
if($event != "")
{
  $org = new Organismo;
  if($event  == 'new')
    $res = $org->crear();
  if($event == 'edit')
    $res = $org->editar();
  if($event == 'del')
    $res = $org->eliminar();
}
$layout = $params->get('layout', 'default');
require JModuleHelper::getLayoutPath('mod_catorg', $layout);

 
?>
 


Notice: Undefined variable: nombre in C:\xampp\htdocs\joomla\modules\mod_catorg\mod_catorg.php on line 26

Notice: Undefined variable: nombre in C:\xampp\htdocs\joomla\modules\mod_catorg\mod_catorg.php on line 27

Notice: Undefined variable: pais in C:\xampp\htdocs\joomla\modules\mod_catorg\mod_catorg.php on line 27

Notice: Undefined variable: descripcion in C:\xampp\htdocs\joomla\modules\mod_catorg\mod_catorg.php on line 27

Notice: Undefined variable: idorganismo in C:\xampp\htdocs\joomla\modules\mod_catorg\mod_catorg.php on line 27

Notice: Undefined variable: db in C:\xampp\htdocs\joomla\modules\mod_catorg\mod_catorg.php on line 28

Fatal error: Call to a member function setQuery() on a non-object in C:\xampp\htdocs\joomla\modules\mod_catorg\mod_catorg.php on line 28


Título: Re: Duda sobre variables
Publicado por: engel lex en 17 Junio 2014, 15:12 pm
Estoy en el cel asi que no veo muy bien, pero casi seguro que estas accediendo a la variable mal

Para acceder a las variabled de otro scope en una misma clase, debea usar $this->variable


Título: Re: Duda sobre variables
Publicado por: marko1985 en 17 Junio 2014, 15:16 pm
Hola,

En el constructor deber llamar a los atributos que has definido con la siguiente sintáxis.

$this->TU_VARIABLE, sino las variables que has definido dentro de __construct se quedan cómo locales y no son visibles al resto de métodos, por otra parte tampoco estarás modificando $idorganismo.

Para acceder a estas variables lo mismo debes llamarlas mediante this->TUVARIABLE
si fueran constantes con self::

Prueba de esta forma

Código
  1. <?php
  2.  
  3. //no direct access
  4. defined('_JEXEC') or die('Acceso restringido');
  5.  
  6. class Organismo
  7. {
  8.  protected $idorganismo=0;
  9.  protected $nombre;
  10.  protected $pais;
  11.  protected $descripcion;
  12.  public      $db;
  13.  
  14.  public function __construct() {
  15.    $this->$idorganismo=$_POST['codigo'];
  16.    $this->$nombre=$_POST['nombre'];
  17.    $this->$pais=$_POST['continente'];
  18.    $this->$descripcion=$_POST['informacion'];
  19.    $this->$db = JFactory::getDBO();  
  20.  }
  21.  public function crear(){
  22.    $query = "INSERT INTO organismos VALUES(" . $this->$idorganismo . ", '" . $this->$nombre . "', '" . $this->$pais . "', '" . $this->$descripcion . "');";
  23.    $this->db->setQuery($query);
  24.    return $$this->db->query();
  25.  }
  26.  public function editar(){
  27.    echo $nombre;
  28.    $query = "UPDATE organismos SET nombre='" . $this->$nombre . "', continente='" . $this->$pais . "', informacion='" . $this->$descripcion . "' WHERE codigo=". $this->$idorganismo . ";";
  29.    $this->$db->setQuery($query);
  30.    return $db->query();
  31.  }
  32.  public function eliminar(){
  33.    $query = "DELETE FROM organismos WHERE codigo='" . $this->$idorganismo . "';";
  34.    $this->$db->setQuery($query);
  35.    return $this->$db->query();
  36.  }
  37. }
  38. $input = new JInput;
  39. $event = $input->get('event', '','string');
  40. $res=false;
  41. if($event != "")
  42. {
  43.  $org = new Organismo;
  44.  if($event  == 'new')
  45.    $res = $org->crear();
  46.  if($event == 'edit')
  47.    $res = $org->editar();
  48.  if($event == 'del')
  49.    $res = $org->eliminar();
  50. }
  51. $layout = $params->get('layout', 'default');
  52. require JModuleHelper::getLayoutPath('mod_catorg', $layout);
  53.  
  54.  
  55. ?>

Espero que no se me haya escapado ningun te recomiento esta página sobre visibilidad en PHP http://www.php.net//manual/es/language.oop5.properties.php


Título: Re: Duda sobre variables
Publicado por: Grave en 17 Junio 2014, 17:18 pm
gracias por la ayuda me ha funcionado :)