0

This is kind of embarrassing but I having problems calling a method from a class in PHP, this is how it goes: I have created a class persona with its respectives properties, the code is

<?php class persona { private $id; private $nombre; //varchar(50) private $correo; //varchar(50) private $especialidad; //varchar(50) private $nacionalidad; //varchar(50) private $sueldo; //float private $isss; //float private $afp; //float //getter public function getId() { return $this->id; } public function getNombre() { return $this->nombre; } public function getCorreo() { return $this->descripcion; } public function getEspecialidad(){ return $this->especialidad; } public function getNacionalidad(){ return $this->nacionalidad; } public function getSueldo(){ return $this->sueldo; } public function getIsss(){ return $this->isss; } public function getAfp(){ return $this->afp; } //setter public function setNombre($nombre) { $this->nombre = $nombre; } public function setCorreo($correo) { $this->correo = $correo; } public function setEspecialidad($especialidad){ $this->especialidad=$especialidad; } public function setNacionalidad($nacionalidad){ $this->nacionalidad=$nacionalidad; } public function setSueldo($sueldo){ $this->sueldo=$sueldo; } public function setIsss($isss){ $this->isss=$isss; } public function setAfp($afp){ $this->afp=$afp; } public function __construct($nombre, $correo, $especialidad, $nacionalidad, $sueldo, $isss, $afp, $id=null) { $this->nombre = $nombre; $this->correo = $descipcion; $this->especialidad = $especialidad; $this->nacionalidad = $nacionalidad; $this->sueldo = $sueldo; $this ->isss = $isss; $this->afp = $afp; $this->id = $id; } public function mostrar(){ $mensaje="hola"; return $mensaje; } } ?> 

as you can see at the end of the code I have created a function called mostrar, the only purpose of this is to show a message, now I want to call this method from a diferent class, my code is

<?php require_once('persona.php'); class prueba{ private $person; public function __construct(){ $person= new persona("el nombre","el correo", "la especialidad", "la nacionalidad", "el sueldo", "el isss", "el afp"); mostrando(); } public function mostrando(){ $person->mostrar(); } } ?> 

but when I debug it doesn' show anything in the browser, I want to display the message from the class person.php, could you please tell me what is the problem in my code?

1
  • 3
    You didn't use an echo anywhere, so yeah, nothing is going to show in the browser. Commented Mar 31, 2015 at 16:54

1 Answer 1

2

Variable scope:

$person !== $this->person

class prueba{ private $person; public function __construct(){ $this->person= new persona("el nombre","el correo", "la especialidad", "la nacionalidad", "el sueldo", "el isss", "el afp"); $this->mostrando(); } public function mostrando(){ echo $this->person->mostrar(); } } 

$this->person is an object property, accessible from all methods in the class.

$person is a local variable, accessible only from the method/function in which it is defined (unless passed as an argument to other methods/functions)

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

1 Comment

You're not doing anything that will show the message.... see my edit, I've added an echo statement.... PHP will only generate output if you tell it to do so

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.