Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,201,644 members, 7,979,190 topics. Date: Friday, 18 October 2024 at 10:52 PM |
Nairaland Forum / Science/Technology / Programming / OOP In PHP [learn /teach] (1012 Views)
Why Is OOP PHP So Confusing? / How Can I Learn OOP With Java? / A Beginners Guide To Object-oriented Programming (OOP) In PHP 5+ (2) (3) (4)
OOP In PHP [learn /teach] by adewasco2k(m): 4:46pm On Jul 21, 2013 |
I am starting this thread not because i know OOP in PHP too well but because it has always been something very confusing to me (implimenting). After learning PHP precudurally, i found the need to learn OO in PHP which was something i just dont get. okay! i learnt the basics but alot of questions still pop in my mind, how to i use it? when do i use it? why should i even use it? before 'WE' start anything or answer the questions we should go over the basics of OOP in PHP (Please i am not an expert, just doing this to help people who are learners of OOP in PHP like me, so the gurus can always add and teach ) -- extracted from wikipedia. Now as an introduction lets just try to explain the above with codes (programmers write codes...lol) 1. An object is a copy or instance of a class, a class holds your attributes (variables), functions (methods). lets get it coded: 1.1 To define a class:
we have just created a class with name "website" 1.2 To create a variable (property) inside a class:
we have now created a variable normally called property in OOP in our website class called 'name', the public in the front just shows it is available to the public to access. (will address in the next lession) 1.3 To create a function (method)
The above code shows how to create a method inside of a class, we created a method with name 'get_namme' and what it simply do is return the variable (property) $name which we created above. you might be wondering what the $this-> does, yes it is only used in a class and it points to a property in a class note how it is been used $this->name; there is no dollar ($) sign before the name. its more like refering to a $name property which is inside the class. 1.4 Accessing the class: now to how we can access the class 'website' out site the class, this is where we would need object, this will be an instance of the class 'website'
we have just set an instance of our class 'website', with a variable outside the class named $object (note, it can be any name), its now a pointer to the class 'website'.
here we have used the $object which is the pointer to the class website to change the property 'name' in the class (Note this is because its a public property, we would address that later)
this will display Nairaland on your screen. Note we also used the $object to access the method 'get_name()' This is the very basic and for beginners. Next lession will address public, private, protected properties, setter and getter. |
Re: OOP In PHP [learn /teach] by adewasco2k(m): 4:54pm On Jul 21, 2013 |
Alot of typos, will correct when i get on PC kinda hard navigating through the lengthy post on this device... pardon me |
Re: OOP In PHP [learn /teach] by Nobody: 12:36pm On Jul 22, 2013 |
After tomorrow exams will be over, so i can shed as much light as i want |
Re: OOP In PHP [learn /teach] by trytillmake(m): 4:54pm On Jul 22, 2013 |
Get concise tutorials on oop here, it will get u walking http://www.tutorialspoint.com/php/php_object_oriented.htm |
Re: OOP In PHP [learn /teach] by Sloan: 8:23am On Jul 27, 2013 |
I am even too sleepy so I am reading and typing with one eye. What happened to the class constructor? PHP does not 'need' a destructor but you should add one if you are going to introduce learners to OOP paradigms and design patterns. Plus, it is GPP - good programming practice! |
Re: OOP In PHP [learn /teach] by oleku(m): 10:30pm On Jul 28, 2013 |
- Your post makes it more confusing and does not represent any concept. - You really need to read PSR-2 (Coding Style Guide)https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md because your class does not follow acceptable Fig Standards which would affect your programming or application in PHP in long term. - This forum does not have code formatting , Why not create a blog with code formatting and tutorials, then add the link here or @seun can create a plugin using http://softwaremaniacs.org/soft/highlight/en/ in this forum |
(1) (Reply)
An Android Social Networking App From A Nigerian - Check It Out! / Php Or Html / 18 Best Online Resources For Learning SQL & Database Concepts
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18 |