PHP Classes

O exemplo para esta classe não funcionou.

Recommend this page to a friend!

      Collection  >  All threads  >  O exemplo para esta classe não...  >  (Un) Subscribe thread alerts  
Subject:O exemplo para esta classe não...
Summary:Package rating comment
Messages:4
Author:Luis Ventura Sousa
Date:2008-04-26 15:53:52
Update:2008-04-27 14:05:35
 

 

Luis Ventura Sousa rated this package as follows:

Utility: Not sure
Consistency: Not sure
Examples: Insufficient

  1. O exemplo para esta classe não...   Reply   Report abuse  
Picture of Luis Ventura Sousa Luis Ventura Sousa - 2008-04-26 15:53:52
O exemplo para esta classe não funcionou.
Não é que eu seja um entendido em PHP (que não sou... já estudei alguma coisa de Java e estou a começar com o PHP...) mas parece-me que falta o método construtor do objecto 'collection'.

  2. Re: O exemplo para esta classe não...   Reply   Report abuse  
Picture of Diogo Souza da Silva Diogo Souza da Silva - 2008-04-26 20:16:07 - In reply to message 1 from Luis Ventura Sousa
Provavelmente ele não achou a classe Collection, acontece que a primeira declaração no arquivo de teste é para incluir o arquivo que contêm a classe, e esta com a estrutura que uso, diferente de como esta no site.

Caso o arquivo Collection.class.php e Collection.test.php estejam na mesma pagina, mude a primeira linha de:

include("../../lang/Collection.class.php");

para:

include("Collection.class.php");

Caso o erro persista me envie a mensagem de erro do php.

  3. Re: O exemplo para esta classe não...   Reply   Report abuse  
Picture of Luis Ventura Sousa Luis Ventura Sousa - 2008-04-27 12:34:14 - In reply to message 2 from Diogo Souza da Silva
<?php

include("Collection.class.php");
// We create an array
$a[] = "Diogo";
$a[] = "Souza";
$a[] = "Silva";

echo "We first create the array : " ;
var_dump($a);
// Them we call a collection, with this array
$col1 = new Collection($a); //ERRO QUANDO TENTA CRIAR O OBJECTO

AO USAR O WAMPSERVER2.0 PARA CORRER O EXEMPLO DÁ:
Collection implements Iterator { public $lenght=0; private $elements = array(); /** * Create a collection of objects */ function __construct($array=null){ if(is_array($array)) { $this->elements = $array ; $this->count(); } else if($array instanceof Collection) { $this->elements = array_merge($this->elements,$array->__toArray()); $this->count(); } } /** * Join(merge) an array or a collection into this one */ function merge($array) { if(is_array($array)) { $this->elements = array_merge($this->elements,$array); } else if(get_class($array) == "Collection") { $this->elements = array_merge($this->elements,$array->elements); } $this->count(); return $this ; } /** * Adds an object into collection * if arg1 only is given, it will be the item at last index * if arg1 and arg2 is given, arg1 will be the index(key) and arg2 will be the value */ function add($arg1,$arg2=false) { if(!$arg2) { $this->elements[] = $arg1; } else { if (!array_key_exists($arg1,$this->elements)) { $this->elements[$arg1] = $arg2; } } $this->count(); return $this ; } /** * Adds value at last index */ function append($value) { return $this->add($value); //return $this ; } /** * Sort by values */ function asort($flags=null) { asort($this->elements,$flags); return $this; } /** * sort by key */ function ksort($flags=null) { ksort($this->elements,$flags); return $this; } /** * sort by natural order */ function sort($flags=null) { sort($this->elements,$flags); return $this ; } /** * count number of itens */ function count(){ $this->lenght = count($this->elements); return $this->length ; } /** * Remove the $key item */ function remove($key) { if (array_key_exists($key,$this->elements)) { unset($this->elements[$key]); $this->count(); return $this; } } /** * Verifies if this key is filled */ function key_exists($k) { return array_key_exists($k, $this->elements); } /** * Moves the cursor a step foward */ function next() { return next($this->elements) ; } /** * Moves cursos to given key */ function seek($index) { $this->rewind(); while($this->valid()) { if($this->key() == "index") { return true; } $this->next(); } return false; } /** * Moves the cursor a step back */ function back() { return prev($this->elements) ; } /** * Puts the cursor at start */ function rewind() { return reset($this->elements); } /** * Puts cursor at the end */ function forward() { return end($this->elemets); } /** * Return the item in the cursor point */ function current() { return current($this->elements); } /** * Returns cursor key */ function currentKey() { return key($this->elements) ; } /** * Actual cursos key */ function key() { return $this->currentKey(); } /** * Check if cursor is at a valid item */ function valid() { if(!is_null($this->key())) { return true; } else { return false ; } } /** * same as valid() */ function isValid() { return $this->valid(); } /** * Returns object for given posistion */ function get($pos) { return $this->elements[$pos]; } /** * Set the value o the given key * return the new element */ function set($key,$item) { $this->elements[$key] = $item; $this->count(); return $this->get($key); } /** * Null all the collection, including keys */ function clear() { $this->elements = array(); $this->length = 0; return $this ; } /** * return the size of the collection */ function size() { return $this->count(); } /** * returns if the collection is empty or not */ function isEmpty() { if($this->count() < 1) return true ; else return false; } /** * check if given object exists in collection */ function contains($obj) { foreach($this->elements as $element) { if($element === $obj) { $this->rewind(); return true ; } } $this->rewind(); return false ; } /** * Return the (first) index(key) of given object */ function indexOf($obj) { foreach($this->elements as $k=>$element) { if($element === $obj) { $this->rewind(); return $k ; } } $this->rewind(); return null ; } /** * returns last index(key) of given object */ function lastIndexOf($obj) { $return = null ; foreach($this->elements as $k=>$element) { if($element === $obj) { $return = $k ; } } $this->rewind(); return $return ; } /** * Return a new collection with a part of this collection */ function subCollection($start,$end) { $new = new Collection(); $i = 0; foreach($this->elements as $k=>$element) { if($i <= $end && $i >= $start) { $new->add($element) ; } $i++ ; } $this->rewind(); return $new ; } /** * Cut the array to given size */ function trimToSize($size) { $t = array_chunk($this->elements,$size,true); $this->elements = $t[0]; $this->length = count($this->elements); return $this ; } /** * Return the xml of this collection */ function __toXml() { $xml = "\n"; foreach($this->elements as $k=>$v) { $xml .= "\n\t"; if(is_string($v)) { $xml.= $v ; } else if(is_object($v) and method_exists($v,"__toXml()")) { $xml.= $v->__toXml() ; } else if(is_object($v) and method_exists($v, "__toString()")) { $xml.= $v->__toString() ; } $xml .= ""; } $xml .= "\n"; $this->rewind(); return $xml ; } /** * A alias to __toXMl() */ function toXML() { return $this->__toXML(); } /** * Returns as an array */ function __toArray() { return $this->elements ; } /** * A alias to __toArray() */ function toArray() { return $this->__toArray(); } /** * Alias to toArray() */ function asArray() { return $this->toArray(); } /** * var_dump the collection */ function __toString() { return "".var_dump($this->elements)."" ; } /** * alias to __toString() */ function toString() { return $this->__toString(); } } ?>We first create the array : array(3) { [0]=> string(5) "Diogo" [1]=> string(5) "Souza" [2]=> string(5) "Silva" } Fatal error: Class 'Collection' not found in C:\wamp\www\Testes\collection\Collection.test.php on line 11

  4. Re: O exemplo para esta classe não...   Reply   Report abuse  
Picture of Diogo Souza da Silva Diogo Souza da Silva - 2008-04-27 14:05:35 - In reply to message 3 from Luis Ventura Sousa
Parece que o wampserver sequer processou o arquivo Collection.class.php como um arquivo php. Experimente abrir no navegador o Collection.class.php ao invés do Collection.test.php, o resultado é que ele não deve mostrar nada.

Se ao abrir o Collection.class.php no navegador amostrar o conteúdo do arquivo, mude a primeira linha deste de <? para <?php , parece que o wampserver vem configurado para aceitar apenas <?php como tag php.

Agora com <?php tente novamente abrir os arquivos e veja o que acontece.