Programmation Orientée Objet avec PHP 5

La dernière version de PHP (version 5 ) offre davantage de possibilités pour la Programmation Orientée Objet (POO). Connaissant déjà cette méthode de programmation (avec d'autres langages tels JAVA ou C++), j'ai eu envie de m'y plonger et de voir ce que peut apporter la POO à un langage de programmation web (php dans mon cas). J'ai donc commencé à programmer quelques fonctionnalités en objet pour les 3 portails de Coconews  : Guadeloupe, Martinique et Guyane. J'ai utilisé la POO pour développer la zone privilège (bientôt en ligne) et le support par ticket.

poo

La POO est, comme son nom l'indique, basée sur la notion d'objet. Un objet possède des attributs (repésentant l'état de l'objet) et des méthodes (représentant des opérations applicables à l'objet). Toute la façon d'imaginer, de penser le code est remis en question par rapport au php classique (dit "procédural"). Il faut un petit temps d'adaptation pour faire du code réellement en objet. La difficulté provient du fait que l'on a parfois du mal à imaginer certaines notions virtuelles sous forme d'objet.

Ce qui est appelée "classe", est tout ce qui définit un objet. Un objet est unique (on parle ici d'instance ou d'occurence de classe) mais deux objets peuvent être instanciés à partir de la même classe. Donc la première chose à faire en POO, c'est de créer une classe (après avoir analyser le problème et savoir ce que l'on veut faire exactement. Voici sommairement la syntaxe à respecter pour créer une première classe (contenu du fichier MaClasse.class.php) :

class MaClasse {
	private $id;
	private $attribut;
	public function __construct($id= null, $attribut= null)
	{
		if(!empty($id)) {
			$this->id = $id;
		}
		if(!empty($attribut)) {
			$this->attribut = $attribut;
		}
	}

	public function affiche()
	{
		echo "L'attribut vaut $this->attribut";
	}
}

Quelques explications sur la classe créée ci-dessus :

  • Une des premières règles de la POO est que le nom de la classe doit commencer par une majuscule. Après avoir indiqué le nom de la classe, on définit les attributs de cette classe (ici les attributs sont id et attribut. La première fonction (__construct) est appelé le constructeur de la classe. C'est grâce à cette fonction que l'on peut créer un objet de cette classe en faisant simplement "new()" (voir ci-après).
  • La variable $this est une variable spécifique à la POO, elle permet d'indiquer que c'est cet objet qui est modifié. $this->id correspond à l'attribut id de l'objet sur lequel on travaille.
  • La deuxième fonction (affiche) est une méthode de l'objet, ici c'est une méthode toute simple qui permet d'afficher, avec une phrase, la valeur de l'attribut nommé attribut.

Une fois la classe créée, il faut pouvoir s'en servir et créer des objets de cette classe :

include("MaClasse.class.php");$objet = new MaClasse(1, 10);$objet->affiche();

La première ligne permet d'inclure le fichier contenant la classe. La deuxième instruction permet de créer un nouvel objet grâce à l'appel au constructeur avec le mot "new". Après cette ligne, on a donc un objet MaClasse (nommé "$objet") dont les attributs id et attribut ont respectivement 1 et 10 comme valeur. Enfin la dernière ligne affiche : "L'attribut vaut 10".

Voici donc le nécessaire pour débuter avec la POO en PHP. Malgré le temps d'adapation à la philosophie de la POO, cette technique de programmation fait gagner du temps. En effet, on se trouve, à la fin, avec un code plus compréhensible (surtout sur des gros projets), plus facilement réutilisable et surtout plus facilement maintenable.

Enfin, je mets quelques liens qui m'ont permis de débuter avec la Programmation Orientée Objet en PHP. Je préfère apprendre avec des exemples concrets, donc la plupart de ces liens vous en présenteront :

Commentaires

  • alternative depobola
    Depobola is one of Indonesia's most significant online soccer betting sites today.
    And why is it necessary to choose Depobola because your
    partner?, because Depobola is one of the particular agents that has a good official license and it is signed up as
    a trusted organization at the center, Depobola is also here because an agency that can be believed to have a new complete
    game.

    Depobola will be also one of the Trusted Gambling Agents, in addition we here
    provide the best service from all experienced customer services professionals who will directly serve you, besides that here we always maintain typically the safety and comfort associated with members in playing on this website, Depobola's website is wrong one website that
    holds interesting events plus promotions that are very necessary for our loyal members, obviously.

    To become Indonesia's greatest soccer dealer, is a good
    online gambling site that already has licenses in addition to licenses as well
    since as an online gambling web site that provides the best services.

    The Advantages of Depobola Along with other Sites
    The established online gambling game that will we provide here
    will be Sportbook, Sportbook is a single of the online gambling related to sports, plus
    Depobola here provides IDENTITY with the best market, besides Sportbook here
    we also supply Livecasino, Slots, Joker123 Fish Shoot, S128 Cockfights,
    Togel Chicken Singapore, Hongkong Togel, Kuala Lumpur Togel, carry
    out not hesitate to register plus get an account from Depobola, to
    Register Depobola is very simple the security of your info is maintained with a security system which is assured.

    Here we provide products who have guaranteed
    safety, the merchandise we provide are SBOBET, MAXBET (IBCBET).
    Both associated with these products have recently been tested from immunity towards cyber crime, phishing, ripoffs, and
    fraud, besides that will Depobola is also one of the biggest on the internet gamblers in Asia.

    Apart from friendly professional customer service, here additionally, you
    will become given a very fast deposit and withdrawal method, not later than a few minutes.

    Depobola also offers backup data where a person can login Depobola from the website that we provide,
    it avoids the improper use of information and maintains the particular security of
    data members.

    To be able to conduct transactions, here Depobola provides complete banks this kind of as
    BCA, BNI, Mandiri, BRI, CIMB, and for transactions here Depobola serves day to day.

    And here Depobola also gives access to members who join our web site to see
    the assertions that have been successfully wager,
    besides members who have joined our website will also get the possibility to take
    part in the sportbook tournament that we all have for free, the sportbook tournament is a
    single of the facilities of which Depobola provides to Depobola loyal members.

    Depobola Alternate Links
    To make this easier for players in order to play soccer gambling over a
    smartphone, the football gambling agent also provides the link
    that can become accessed anytime and anyplace needless to
    say with the accessibility of an alternative depobola link.
    Considering that today there is a lot of positive internet in Indonesia, the provider always provides up to date alternative links in purchase to smooth the users to the original internet site.

    Besides the positive internet presently there are also several other soccer agents sometimes ignorant about behalf of the depobola alternative site.
    So that makes us always recommend that loyal members should really make sure that the internet site they will visit is depobola.
  • Wayne
    • 2. Wayne Le Sam 29 fév 2020
    Do you mind if I quote a few of your articles as long as I provide credit
    and sources back to your blog? My blog is in the very same area of interest as yours and my visitors would certainly benefit from
    a lot of the information you provide here. Please let me know if this
    ok with you. Thanks a lot!
  • xe cứu hộ
    I've been exploring for a bit for any high-quality articles or weblog posts in this
    sort of area . Exploring in Yahoo I eventually stumbled upon this web site.
    Reading this information So i am satisfied to express that I've an incredibly excellent uncanny
    feeling I found out just what I needed. I most definitely will make
    sure to don?t forget this website and give it a glance on a constant basis.
  • link Betberry indonesia
    It's truly very complicated in this full of
    activity life to listen news on Television, therefore I just use web for that purpose,
    and take the hottest news.
  • judi Online Baccarat
    Betmaya menyediakan layanan on-line having a bet agent untuk bola dan on line casino dengan seperti sbobet, sbobet on line casino, sbobet agen on line casino 338a
    cara daftar. Betmaya menyediakan layanan on line having a
    bet agent untuk bola dan casino dengan seperti sbobet, sbobet on line casino, sbobet agen on line casino 338a cara daftar.

    Spbobet adalah pusat penyengara judi online, live on line casino online, togel on-line yang terpercaya di asia.
    Katanabet merupakan situs judi agen bola on-line terpercaya memberikan pendaftaran akun taruhan bola, agen bola, online casino, togel online
    dan sabung ayam. Daftar sbobet indobetplay menyediakan pembukaan akun baru taruhan judi bola on line casino
    dan slots dari situs judi bola sbobet melalui agen bola
    indobetplay. Daftar sbobet bola on line casino 338a dan slots.
  • this is trash meme turning in my assignment
    Hello there, You've done an excellent job. I will certainly digg it and in my view suggest to my friends.
    I'm sure they will be benefited from this web site.
  • holiday Lettings
    I got what you mean, appreciate it for putting up. Woh I am happy
    to find this website through google.
  • rodeo bull Sunderland
    Thank you, I've recently been searching for information approximately this topic for a long tkme and yours is the greatet I've came
    upon so far. However, what in regards to the bottom
    line? Aree you sure about the supply?
  • recycled aggregate
    Really no matter if somjeone doesn't know afterward
    its up to other visitors that they will help, so hedre it takes place.
  • motorhome dealers Devon
    Hi, its pleasant paragraph on the toppic of mefia print, we all know merdia
    is a impressive source of data.
  • corporate business travel management services
    I alwys was concerned in this subject annd still am, appreciate it
    for putting up.
  • bouncy castle hire Sunderland Tyne and Wear
    I am truly grateful to the owner of this website who has shared this fahtastic paragraph at
    here.
  • computer hardware Newcastle
    Superb, what a blog iit is! This website gives helpful data to us, keep iit up.
  • motorhome dealers Brighton
    Aw, this was an exceptionally good post. Taking a few minutes
    and actual effort to create a really good article? but what can I say?
    I hesitate a lot and don't seem to get narly ajything done.
  • i want to sell my company
    Hello. magnificent job. I did nott anticipate this. This is
    a great story. Thanks!
  • managed server hosting UK
    I love reading through and I believe this website got some reakly usefl stguff
    on it!
  • market property
    Thank you for the sensible critique. Me & my neighbor were just preparing to do some research oon this.

    We got a grab a book from our area library but I think
    I learned more clear from this post. I'm very glad to
    seee suchh excelklent info being shared freely ouut there.

Voir plus de commentaires

Ajouter un commentaire