Foros del Web » Programando para Internet » PHP » Frameworks y PHP orientado a objetos »

PHP OO Llamar o hacer referencia a una propiedad

Estas en el tema de Llamar o hacer referencia a una propiedad en el foro de Frameworks y PHP orientado a objetos en Foros del Web. Practica: Simple clase para leer archivo de texto linea por linea. Duda: ¿Cómo puedo llamar a $thiss->line dentro del metodo view();? El objetivo es ver ...
  #1 (permalink)  
Antiguo 30/11/2011, 23:57
 
Fecha de Ingreso: abril-2009
Mensajes: 46
Antigüedad: 15 años
Puntos: 1
Llamar o hacer referencia a una propiedad

Practica: Simple clase para leer archivo de texto linea por linea.

Duda:
¿Cómo puedo llamar a $thiss->line dentro del metodo view();? El objetivo es ver cada linea.

Ej.
Código PHP:
Ver original
  1. public function view() {
  2.         return $this->line;
  3.     }

Código PHP:
<?php
class file {

    private 
$path;
    private 
$file;
    private 
$finalpath;
        private 
$line;
    
    public function 
__construct($ruta$filename) {
        
$this->path $ruta;
        
$this->file $filename;
        
$this->finalpath $this->path "/" $this->file;
    }
    
    
    public function 
read() {
        
        if (
is_dir($this->path) && is_file($this->finalpath)) {
            return 
file($this->finalpath);
        }
        
        return 
false;    
    }
    
    public function 
walk() {
        
        if (
is_array($this->read())) {
            foreach (
$this->read() as $value) {
                
$this->line .= $value;
            }
            return 
$this->line;
        }
    }

    public function 
view() {
        return 
$this->walk();
    }
    
}
?>

Última edición por opzina2; 01/12/2011 a las 00:16
  #2 (permalink)  
Antiguo 01/12/2011, 08:13
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Llamar o hacer referencia a una propiedad

Pero si ya lo hiciste ¿Es un aporte o una pregunta? Si es un aporte, trata siempre de colocar al principio algo como [APORTE] y luego el resto del título. Y si es una tarea y estás dando la respuesta no estás ayudando positivamente. Estas ayudando negativamente a crear vagos
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos
  #3 (permalink)  
Antiguo 01/12/2011, 09:08
 
Fecha de Ingreso: abril-2009
Mensajes: 46
Antigüedad: 15 años
Puntos: 1
Respuesta: Llamar o hacer referencia a una propiedad

Cita:
Iniciado por abimaelrc Ver Mensaje
Pero si ya lo hiciste ¿Es un aporte o una pregunta? Si es un aporte, trata siempre de colocar al principio algo como [APORTE] y luego el resto del título. Y si es una tarea y estás dando la respuesta no estás ayudando positivamente. Estas ayudando negativamente a crear vagos
Cita:
Pero si ya lo hiciste
Código PHP:
Ver original
  1. public function view() {
  2.         return $this->line;
  3.     }

El tema es que cuando instancio el objeto, al llamar al metodo view(), no me trae lo que contiene $this->line;
  #4 (permalink)  
Antiguo 01/12/2011, 09:18
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Respuesta: Llamar o hacer referencia a una propiedad

Te faltaría llamar a $this->walk() para hacerlo, aunque no me gusta mucho ese método ya que recorre 2 veces el archivo, yo lo haría así:

Código PHP:
Ver original
  1. public function walk() {
  2.         if (is_dir($this->path) && is_file($this->finalpath)) {
  3.             $this->line = file_get_contents($this->finalpath);
  4.         }
  5.     }
  #5 (permalink)  
Antiguo 01/12/2011, 23:15
 
Fecha de Ingreso: abril-2009
Mensajes: 46
Antigüedad: 15 años
Puntos: 1
Respuesta: Llamar o hacer referencia a una propiedad

Gracias, Dejo la ultima corrección por si a caso.

Código PHP:
Ver original
  1. <?php
  2. class file {
  3.  
  4.     private $root;
  5.     private $filename;
  6.    
  7.     public function __construct($ruta, $filename) {
  8.         $this->root = $ruta;
  9.         $this->filename = $this->root . "/" . $filename;
  10.     }
  11.    
  12.     public function read() {
  13.        
  14.         if (is_dir($this->root) && is_file($this->filename)) {
  15.             $lineas = file($this->filename);
  16.            
  17.             for($i = 0; $i < count($lineas); $i++) {
  18.                 $linea .= $lineas[$i] . "<br />";
  19.             }
  20.            
  21.             return $linea;
  22.         }
  23.        
  24.     }
  25.  
  26.     public function view() {
  27.         return $this->read();
  28.     }
  29.    
  30. }
  31. ?>

Etiquetas: php, propiedad, referencia
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:31.