0

I am working with two classes: usuarios, preguntas.

In preguntas I store id_usuario which correspond to the id from user, ok. But sometimes I need to display more than the id, so I made a function in usuarios. php to print this info:

This is mi code for now

usuarios.php (I'm only including relevant code for this problem)

Código PHP:

function __construct($id){

    $consulta = mysql_query("SELECT * FROM usuarios  WHERE id = '".$id."'");
        while($item = mysql_fetch_array($consulta)){                              
            $this->id = $item['id'];
            $this ->fid = $item['fid'];
            $this ->usuario = $item['alias'];
            $this ->password = $item['pass'];
            $this ->email = $item['mail'];
            $this ->fechar = $item['fechar'];
            $this ->ultima = $item['ultima'];
            $this ->img_src = $item['img_src'];
            $this ->reputacion =     $this ->fechar = $item['reputacion'];
        }
}




function miniatura(){
    $html_mini = "<div>$this->usuario</div>";
    return $html_mini;

} 

pregunta.php (i'm only including relevant code for this problem)

Código PHP:

function get_autor(){


                $us = new usuario($item['id']);

            return $us->miniatura();
}



function imprimir_titular(){
    $html_t = '<h1 class="prg'.$this->id.'" >[ '.$this->id_eval_q.' ] '.$this->get_autor().' pregunta: '.$this->pregunta.' , '.$this->fecha.'</h1>';
    return $html_t;
} 

And this is the error:

Cita:

Fatal error: Call to undefined method usuario::miniatura() in /home/piscolab/public_html/keepyourlinks.com/recetorium/clases/pregunta.php on line 35 No entiendo por qué no accede al método de la clase usuarios, aunque me deje crear el objeto usuario :S

Details: - Protected atributes

Any help will be wellcome

4
  • 1
    Where is the definition of the usuario class? Is it supposed to be around the first code snippet? Commented Dec 18, 2010 at 15:49
  • try making the method "public". - public function miniatura(){ } Commented Dec 18, 2010 at 16:11
  • i tried making it public and it gives me an error 'unexpedted T_string public' :S Commented Dec 18, 2010 at 20:34
  • the definition of usuarios it's included in the main file (i tried including it aswell in the pregunta class but it gives me an error 'you cant redeclare __construct....' so it's included propperly :S Commented Dec 18, 2010 at 20:35

2 Answers 2

1

I copied you code, change content of methods and everything works

    class usuario {
function __construct($id){

    echo 'ok';
}




function miniatura(){
    echo 'ok';

}   

}




function get_autor(){


                $us = new usuario($item['id']);

            return $us->miniatura();
}

Show full classes because with Your code as shown in nothing wrong.

Sign up to request clarification or add additional context in comments.

1 Comment

does what i provided is enough?
0

ok, this is the file where i'm calling both:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Recetorium > Preguntas - Pregunta o responde qué y cómo cocinar algo

Cargando..

and in router.php

<?php require_once('funciones.php');          

if(isset($_POST['inicio'])){

// el usuario está iniciando sesion         

$iniciando = new sesion_usuarios();
if($iniciando->iniciar()){
    imprimir_sesion_iniciada();
}else{
    imprimir_formulario_sesion();
}

}else if(isset($_POST['registro'])){ $registrando = new registro_usuarios(); if($registrando->register()){ imprimir_usuario_registrado(); }else{ imprimir_formulario_registro(); }

}else if(isset($_GET['que']) or isset($que)){

    if(isset($que))
        $tarea = $que;
    else
        $tarea = $_GET['que'];

    if($tarea == 'registro'){
        imprimir_formulario_registro();
    }else if($tarea == 'login'){
        imprimir_formulario_sesion();
    }else if($tarea == 'salir'){
        cerrar_sesion();
    }else if($tarea == 'ultimas_preguntas'){
        listar_preguntas();
    }else if($tarea == 'nueva_pregunta'){
        $tem = new pregunta();
        $tem->imprimir_formulario;
    }else if($tarea == 'ultimas_recetas'){
        $tem = new pregunta();
        $tem->imprimir_formulario;
    }

}else if(sesion()){ echo 'Pronto prodrás: Preguntar cosas, responder cosas y evaluar ambos. Publicar tus recetas, descubrir otras, evaluarlas y ser evaluado.'; }else{ $archivo = 'bienvenida.php';
include($archivo); imprimir_formulario_sesion();

}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.