FAQ |
Kalender |
|
![]() |
#1 | ||
|
|||
Klarade millennium-buggen
|
Jag har brutit ut den generella egenskapen att hantera getters och setetrs och lagt det i en abstract Rootklass så andra klasser kan ärva in beteendet och vissa egenskaper.
Så här blev det i min testkod: Kod:
<?php /////////////////////////////////////////////////////////////////////////////// // File: AbstractRoot.php // Class: AbstractRoot /////////////////////////////////////////////////////////////////////////////// abstract class AbstractRoot { private $_ChildClassname; public function __construct($ChildClassname) { $this->ChildClassname = $ChildClassname; } public function __set($name, $value) { switch ($name) { case 'ChildClassname': $this->_ChildClassname = $value; break; default: // By throwing Exception for undefined property we can enforce strict property definition rules // we also achive better error messages for improved debugging during development // this is necessare because of lack of support for strict properties in php 5.3.15 $error = "Error: __set property: " . $name . " not supported by class: " . $this->ChildClassname . "." ; throw new Exception($error); break; } } public function __get($name) { switch ($name) { case 'ChildClassname': return $this->_ChildClassname; break; default: // By throwing Exception for undefined property we can enforce strict property definition rules // we also achive better error messages for improved debugging during development // this is necessare because of lack of support for strict properties in php 5.3.15 $error = "Error: __get property: " . $name . " not supported by class: " . $this->ChildClassname . "."; throw new Exception($error); break; } return null; } } ?> Kod:
<?php /////////////////////////////////////////////////////////////////////////////// // File: Person.php // Class: Person /////////////////////////////////////////////////////////////////////////////// class Person extends AbstractRoot { private $firstname; private $lastname; public function __construct($pFirstname, $pLastname) { parent::__construct(__CLASS__); // Always $this->firstname = $pFirstname; $this->lastname = $pLastname; } /* a String representation for every Person. */ public function __toString() { return $this->firstname . " " . $this->lastname; } } ?> Kod:
<?php /////////////////////////////////////////////////////////////////////////////// // File: PersonAge.php // Class: PersonAge /////////////////////////////////////////////////////////////////////////////// class PersonAge extends AbstractRoot { private $firstname; private $lastname; private $age; public function __construct($pFirstname, $pLastname, $pAge) { parent::__construct(__CLASS__); // Always $this->firstname = $pFirstname; $this->lastname = $pLastname; $this->age = $pAge; } /* a String representation for every PersonAge. */ public function __toString() { return $this->firstname . " " . $this->lastname . " (" . $this->age . ")"; } } ?> Kod:
<?php /////////////////////////////////////////////////////////////////////////////// // File: PropertiesTest.php // Test for Class: AbstractRoot, Person, PersonAge /////////////////////////////////////////////////////////////////////////////// class PropertiesTest { public function __construct() { try { echo "\nTest of errormessage wehen faulty property ...\n"; echo "\nStart testing class: Person.\n---------------------------------------\n\n"; $rad = 0; echo "(row) - Firstname Lastname\n"; # create a PHP Array and initialize it with Person objects $persons = array ( new Person("Fredrik", "Framberg"), new Person("Greta", "Gavelstam"), new Person("Urban", "Urberg"), new Person("Anna", "Ambtesteg"), new Person("Henrik", "Hammarberg"), new Person("Kristina", "Karlestam"), new Person("Hans", "Hallin"), new Person("Berit", "Bygdén") ); # print out the results - calls Person->__toString(). foreach($persons as $person) echo "(" . ++$rad . ") - " . "$person\n"; } catch (Exception $e) { echo 'Caught exception (1): ', $e->getMessage(), "\n"; } try { echo "\nTest of creating new object of class Person ...\n"; $faultyPerson = new Person("Dennis","Harper"); echo "This message will hopefully be written to console.\n"; } catch (Exception $e) { echo 'Caught exception (2): ', $e->getMessage(), "\n"; } try { echo "\nTest of errormessage when faulty __set property ...\n"; echo "(" . ++$rad . ") - " . "$faultyPerson\n"; $faultyPerson->Age = 34; $faultyPerson->ShoeSize = 45; echo "Age: " . $faultyPerson->Age . " years.\n"; echo "ShoeSize: " . $faultyPerson->ShoeSize . ".\n"; echo "This message will not be written to console.\n"; } catch (Exception $e) { echo 'Caught exception (3): ', $e->getMessage(), "\n"; } try { echo "\nTest of errormessage when faulty __get property ...\n"; echo "(" . ++$rad . ") - " . "$faultyPerson\n"; echo "ShoeSize: " . $faultyPerson->ShoeSize . ".\n"; echo "Age: " . $faultyPerson->Age . " years.\n"; echo "This message will not be written to console.\n"; } catch (Exception $e) { echo 'Caught exception (4): ', $e->getMessage(), "\n"; } echo "\nDone testing class: Person.\n---------------------------------------\n\n"; echo "\nStart testing class: PersonAge.\n---------------------------------------\n\n"; try { echo "\nTest of errormessage when faulty __set property ...\n"; $faultyPersonAge = new PersonAge("Arthur","Trent",47); echo "(" . ++$rad . ") - " . "$faultyPersonAge\n"; $faultyPersonAge->age = 58; $faultyPersonAge->ShoeSize = 43; echo "ShoeSize: " . $faultyPersonAge->ShoeSize . ".\n"; echo "Age: " . $faultyPersonAge->age . " years.\n"; echo "This message will not be written to console.\n"; } catch (Exception $e) { echo 'Caught exception (5): ', $e->getMessage(), "\n"; } try { echo "\nTest of errormessage when faulty __get property ...\n"; //$faultyPersonAge = new PersonAge("Gary","Middleton",56); echo "(" . ++$rad . ") - " . "$faultyPersonAge\n"; echo "Age: " . $faultyPersonAge->age . " years.\n"; echo "ShoeSize: " . $faultyPersonAge->ShoeSize . ".\n"; echo "This message will not be written to console.\n"; } catch (Exception $e) { echo 'Caught exception (6): ', $e->getMessage(), "\n"; } echo "\nDone testing class: PersonAge.\n---------------------------------------\n\n"; } } try { $test = new PropertiesTest(); } catch (Exception $e) { echo 'Caught exception (7): ', $e->getMessage(), "\n"; } ?> |
||
![]() |
![]() |
![]() |
#2 | ||
|
|||
Medlem
|
Citat:
Dummaste jag sett på länge.. Antingen kan du språket/vet vad du gör (oavsett programspråk) eller så kan/vet du inte.. Vad är det för mening med att sätta 200 parametrar som ändå inte används? PHP-kod:
Bygga in skydd/lager-på-lager-på-lager-på-lager mot klantskallar kan ju Java/.NET fortsätta hålla på med tills dom avlider.. Förväntar du dig ett viss datatyp när du sätter en property så får du väl kolla efter det istället? Inte bara om den ens finns "definierad" i klassen. Senast redigerad av Jake.Nu den 2012-07-26 klockan 01:48 |
||
![]() |
![]() |
![]() |
#3 | ||
|
|||
Klarade millennium-buggen
|
Citat:
Om du inte begriper objektorienterad programmering så bör du inte komma med fullkomligt idiotiska inlägg här i forumet. |
||
![]() |
![]() |
![]() |
#4 | ||
|
|||
Medlem
|
Citat:
Du vet inte hur PHP (eller dess objekthantering) fungerar så hur skulle du kunna förstå vad jag menade.. Jag begriper däremot inte varför du ens ger dig på att försöka använda PHP, du är så inställd på hur typade och kompilerade språk fungerar och måste jämföra allt med dessa hela tiden. Senast redigerad av Jake.Nu den 2012-07-31 klockan 08:05 |
||
![]() |
![]() |
![]() |
#5 | ||
|
|||
Administratör
|
Citat:
Din abstrakta klass verkar lite omständig. Med switch-satsen måste du uppdatera root-klassen med alla properties för alla underklasser. Du har redan fått ett vettigt exempel med isset() istället, som du faktiskt kan lägga som en abstrakt klass utan att få massa switch cases på ett helt ologiskt ställe. Använder du PHP5.4 kan du använda den som en trait (tyvärr är den inte helt redo för alla produktionsmiljöer pga extensions som apc och patches som suhoshin). Det finn en väl vedertagen standard för hur man skriver kommentarer för att kunna generera dokumentation i PHP - titta på phpdocumentor. Liksom för testning har du PHPUnit (allra mest välanvänt) som gör vad du gör i din testklass, fast smidigare, bättre output och enklare att integrera i byggservrar osv. Dynamiska properties är en styrka hos PHP. Överanvändning av det är en svaghet hos programmeraren. Med början i PHP5.4 finns det en stark anledning till att bli striktare med det då de har optimerat hanteringen av fördefinierade properties rejält.
__________________
eldefors.com - Personlig (teknik)-blogg |
||
![]() |
![]() |
Svara |
|
|