Your SlideShare is downloading. ×
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Oop koncepti
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Oop koncepti

188

Published on

OOP Concepts by Ivana Petrovic

OOP Concepts by Ivana Petrovic

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
188
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. PHP Object Oriented ProgrammingConcepts, Patterns, Examples
  • 2. UVOD – Ukratko o OOP•Feksibilnost, brzina kodiranja, brzina rada aplikacije,organizacija koda za buduce izmene I dorade.•Ne moze se zamisliti velika I kompleksna aplikacija bezOOP pristupa izrade.•Laksa vizualizacija citave aplikacijeclass Automobil { public $marka; public $cena; public function vozi($gde){ return ‘Auto ’.$this->$marka.’ koji je cene ’.$this->$cena.’ RSD vozim ’.$gde; }}//upotreba $auto = new Automobil;$auto->marka = ‘Trabant’;$auto->cena = ‘100’;$auto->vozi(‘na otpad’);//odgovor: Auto Trabant koji je cene 100RSD vozim na otpad
  • 3. Koncepti OOP•Klasa•Konstruktor•Konstanta klase•Nasledjena klasa•Mutacije•Autoloaderi•Staticne varijable I metode•Polimorfizam
  • 4. Klasa•Templejt, okvir jednog objektaNpr.: Voce je klasa, dok su limuni, jabukeobjekti klase Voce.•class Website { public function add_website($params = array()){ //checking and inserting into db }}Website::add_website(array(‘url’=>’http://www.nekisajtovo.ono’, ‘user’ => ‘admin’));
  • 5. Konstruktor•Poziva se prilikom kreiranja objekta klase•Moze se iskoristiti za skladistenje globalnihvrednosti za klasu•class Website { function __construct(){ print ‘Dodjoh u klasu’; } public function add_website($params = array()){ //checking and inserting into db }}Website::add_website(array(‘url’=>’http://www.nekisajtovo.ono’, ‘user’ => ‘admin’));Rezultat: Dodjoh u klasu
  • 6. Konstanta klase•Vrednosti koje mozemo da koristimo bilogde unutar klase•class Website { const verzija = ‘3.2’; function __construct(){ print ‘Dodjoh u klasu’; } public function add_website($params = array()){ echo ‘<br />Website mora biti u verziji ’.self::verzija.’ +’; //checking and inserting into db }}Website::add_website(array(‘url’=>’http://www.nekisajtovo.ono’, ‘user’ => ‘admin’));Rezultat: Dodjoh u klasuWebsite mora biti u verziji 3.2 +
  • 7. Nasledjena klasaDobija se vise fleksibilnosti u programiranjuS druge strane ne treba ih koristiti kadimamo samo razlicite vrednosti izmedjuklasa, treba da ih koristimo samo kad jestruktura I ponasanje klasa drugacije. Trebapreferirati objektnu kompoziciju, naspramnasledjivanja klasa.
  • 8. Mutacije•Koristi se za promeni vrednosti privatnihmetoda iz public funkcije•class Website { private $verzija; public function change_version($arg){ $this->verzija = $arg; } public function add_website($params = array()){ echo ‘<br />Website mora biti u verziji ’.$this->verzija.’ +’; //checking and inserting into db }}Website::change_version(‘3.2.5’);Website::add_website(array(‘url’=>’http://www.nekisajtovo.ono’, ‘user’ => ‘admin’));Rezultat:Website mora biti u verziji 3.2.5
  • 9. AutoloaderiOlaksava posao inkludovanja fajlovaJednim funkcija moze da hendluje sveincludesfunction _autoload($classname){ require_once($classname.’.php’);}$classname = ‘lib/mailchimp.api.class’;$mailhimp = new $classname();
  • 10. Staticne varijable I metodeStaticne varijable I metode se koriste kadaimamo neki sadrzaj ili metodu koju hocemokao takvu da delimo medju svim objektimaklaseUkoliko imamo staticne metode onda jojpristupamo preko klase, a ne preko objekta:Website::add_website($siteinfo);
  • 11. Polimorfizam•Nacin koji omogucava klasama da imajurazlicitu funkcionalnost dok dele slicniinterfejs.interface MyInterface { public function doThis(); public function doThat(); public function setName($name);}class MyClass implements MyInterface { protected $name; public function doThis() { // code that does this } public function doThat() { // code that does that } public function setName($name) { $this->name = $name; }}
  • 12. Paterni•Templejti koji nam olaksavaju resenjeproblema•Ima 23 paterna trenutno, I kategorisani suna “Creation” paterne, “Structural”, I“Behavioral” paterne.•5 najcesce koriscenih: “Factory Method”,“Singleton Method”, “Observer Method”,“The chain-of-command pattern”, “Thestrategy pattern”
  • 13. Factory Method•Dobrodosao je u velikim sistemima, kadajedna izmena koda moze da poremeti citavdalji niz aplikacije.•Ukoliko treba da se izmeni tip objekta uglavnoj klasi, treba samo da se promenifactory klasa, I onda ce sve ostale klase dakoristi novi objekat preko factory klase,automatski.
  • 14. •interface IUser { function getName();}class User implements IUser { public function __construct( $id ) { } public function getName() { return "Zeljko"; }}class UserFactory { public static function Create( $id ) { return new User( $id ); }}$uo = UserFactory::Create( 1 );echo( $uo->getName()."n" ); //response: Zeljko
  • 15. Singleton Method•Sluzi da jednu klasu instancira samojednom, ako je potrebno, kako se ne bigubilo vreme.
  • 16. Observer Method•Jos jedan nacin sprecavanja kolapsa prilikoizmene koda.•Dobijamo metodu koja dozvoljava drugimobjektima, posmatracima, da se registruju.Kada se posmatrani objekat promeni,posalje poruku svim registrovanimposmatracima, pa oni dalje mogu da radesta hoce s tim podacima.
  • 17. •interface IObserver { function onChanged( $sender, $args );}interface IObservable { function addObserver( $observer );}class UserList implements IObservable { private $_observers = array(); public function addCustomer( $name ) { foreach( $this->_observers as $obs ) $obs->onChanged( $this, $name ); } public function addObserver( $observer ) { $this->_observers []= $observer; }}class UserListLogger implements IObserver { public function onChanged( $sender, $args ) { echo( "$args added to user listn" ); }}$ul = new UserList();$ul->addObserver( new UserListLogger() );$ul->addCustomer( "Zeljko" );
  • 18. The chain-of-command pattern•Omogucava da se lancano pozovu komande, posalju poruke, zahtevi ili sta god.•interface ICommand { function onCommand( $name, $args );}class CommandChain { private $_commands = array(); public function addCommand( $cmd ) { $this->_commands []= $cmd; } public function runCommand( $name, $args ) { foreach( $this->_commands as $cmd ) { if ( $cmd->onCommand( $name, $args ) ) return; } }}class UserCommand implements ICommand { public function onCommand( $name, $args ) { if ( $name != addUser ) return false; echo( "UserCommand handling addUsern" ); return true; }}class MailCommand implements ICommand { public function onCommand( $name, $args ) { if ( $name != mail ) return false; echo( "MailCommand handling mailn" ); return true; }}$cc = new CommandChain();$cc->addCommand( new UserCommand() );$cc->addCommand( new MailCommand() );$cc->runCommand( addUser, null );$cc->runCommand( mail, null );
  • 19. The strategy pattern•Omogucava da se uzme deofunkcionalnosti iz komplekse klase, kako bimogli lakse da se menjaju, bez uticaja naostatak koda.

×