Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [skrypt] Obłsuga Typów w php, Oceńcie skrypt oraz pomysł
Jabol
post 25.07.2004, 20:54:31
Post #1





Grupa: Przyjaciele php.pl
Postów: 1 467
Pomógł: 13
Dołączył: 22.02.2003

Ostrzeżenie: (0%)
-----


Oto moje skrypty (init.php):
  1. <?php
  2.  
  3. require_once(dirname(__FILE__).'/PhpType.class.php');
  4. require_once(dirname(__FILE__).'/ArrayType.class.php');
  5. require_once(dirname(__FILE__).'/BooleanType.class.php');
  6. require_once(dirname(__FILE__).'/CallableType.class.php');
  7. require_once(dirname(__FILE__).'/FloatType.class.php');
  8. require_once(dirname(__FILE__).'/IntType.class.php');
  9. require_once(dirname(__FILE__).'/NullType.class.php');
  10. require_once(dirname(__FILE__).'/ObjectType.class.php');
  11. require_once(dirname(__FILE__).'/ResourceType.class.php');
  12. require_once(dirname(__FILE__).'/ScalarType.class.php');
  13. require_once(dirname(__FILE__).'/StringType.class.php');
  14. require_once(dirname(__FILE__).'/EBadType.class.php');
  15.  
  16. ?>
PhpType.class.php
  1. <?php
  2.  
  3. abstract class PhpType
  4. {
  5. protected $_var;
  6.  
  7. abstract protected function _check();
  8. public function __construct($_var)
  9. {
  10. $this->_var=$_var;
  11. if(!$this->_check()) throw new EBadType('Bad Type');
  12. }
  13. public function __get($_name)
  14. {
  15. return $this->_var;
  16. }
  17. public function __set($_name, $_val)
  18. {
  19. $ret=$this->_var;
  20. $this->_var=$_val;
  21. return $ret;
  22. }
  23. }
  24.  
  25. ?>
EBadType.class.php
  1. <?php
  2.  
  3. class EBadType extends Exception
  4. {
  5. }
  6.  
  7. ?>
IntType.class.php
  1. <?php
  2.  
  3. class IntType extends PhpType
  4. {
  5. protected function _check()
  6. {
  7. return is_int($this->_var);
  8. }
  9. }
  10.  
  11. ?>
I tak dalej już idą typy. Jeżeli chcecie całość to proszę pisać na pw, a wyśle spakowane na maila.
Tutaj test
  1. <?php
  2.  
  3. function requiresint(IntType $_int)
  4. {
  5. print $_int->var;
  6. $_int->var=8;
  7. print $_int->var;
  8. }
  9.  
  10. require_once('types/init.php');
  11. print &#092;"IntType(5)n\";
  12. $int=new IntType(5);
  13. print &#092;"ArrayType(array(10))n\";
  14. $arr=new ArrayType(array(10));
  15. print &#092;"BooleanType(TRUE)n\";
  16. $bln=new BooleanType(TRUE);
  17.  
  18. try
  19. {
  20. print &#092;"ResourceType(NULL)n\";
  21. $res=new ResourceType(NULL);
  22. }
  23. catch (EBadType $e)
  24. {
  25. print $e;
  26. print &#092;"nError catchedn\";
  27. }
  28.  
  29. requiresint($int); // OK
  30. requiresint($bln); // BŁĄD php
  31.  
  32. ?>
Proszę o ocene i ewentualne komentarze, pomysły.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 11:13