Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,241 members, 7,818,825 topics. Date: Monday, 06 May 2024 at 06:00 AM

OOP In PHP [learn /teach] - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / OOP In PHP [learn /teach] (998 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)

(1) (Reply) (Go Down)

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 )


by the way OOP means Object Oriented Programming which is a programming paradigm that represents concepts as 'objects' that have data fields (
attributes that describe the object) and associated procedures known as methods. object which are instance of classes, are used to interact with one
another to design applications and computer programs (jave, c#, php etc)
-- 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:

<?php

class website{

}

?>

we have just created a class with name "website"


1.2 To create a variable (property) inside a class:

<?php
class website{

public $name;

}


?>

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)

<?php
class website{

public $name;

function get_name(){
return $this->name;

}

}
?>

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'

<?php
$object = new 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'.

<?php

$object->name = 'Nairaland';

?>

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)

<?php

echo $object->get_name();

?>

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)

When Will Facebook And Nairaland Finally Introduce The "Unlike" Button?.... / Login Username From One Table And Password From Table 2 / Control Your Room Light Bulb Over The Internet Using A Smart Phone

(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. 17
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.