Object oriented programming in PHP

Started on the old forum in December 2011 · view the original in the archive · old links redirect here

6 replies · 411 views · started 14 years ago · 1 min read

5 participants
softwarefreak Senior · 349 posts #1

Hi guys,I was trying to figure out the advantages of OOP in php,but still not getting a good reason to make it use in real work.

Wat I'm trying to say is,

class introduceYourself

{
private $_name;

public function __construct($name)
{
$this->_name = $name;
}
public function writeName()
{
echo 'My name is ', $this->_name, .'<br />';
}
}

$intro = new introduceYourself("someone");
$intro->writeName();
///////////////////////////////////
output://////////////////////////
My name is someone///////////
/////////////////////////////////

Can someone demonstrate a superb example where,use of classes will really make sense:?

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

GumSlone Administrator Principal · 1,690 posts #2

i think it shoulb be like this

$i = new introduceYourself("someone");$intro = $i->writeName();

there are several advantages in oop, you can use functions with similar names in different classes, also you dont need to define varibles every time to the functions in classes. eg. if private $_name; is set, you can request its value in every function simply by using $this->_name;

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

softwarefreak Senior · 349 posts #3

you can use functions with similar names in different classes, also you dont need to define varibles every time to the functions in classes.

Thanks,2 advantages making sense

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

Ponick Developer · 216 posts #5

Im writting a varry basic understanding of oop

class Car{
var $wheels = 4;
var $doors = 4;

function wheelsdoors()
{
return $this->wheels + $this->doors;
}

}

$car=new car();

echo $car->wheels."<br/>";
echo $car->doors."<br/>";
echo $car->wheelsdoors() ."<br/>";
/* I don't know everything hehe */

Find me on facebook

ksg91 Developer · 162 posts #6

OOP has serveral advantage over POP. To mention few

  1. The system you are developing reflects real world model and so it will be quite easy to implement (However pain in 4$$ if you dont understand OOP concepts and cant visualize the system)
  2. This code is modular and so modification and maintaining code becomes easy.
  3. You can re use the code, to achieve maximum re usability and so it reduce redundant code and so less efforts required.

Some more i can say is security, abstraction, and lot more.

All this I mentioned is just little part of advantage of whole OOP paradigm, advantages are much more.

Added after 10 minutes:

The concept you have currently in your mind is not pecfectly true. In OOP, we map real world entities (objects) into class. Class will also reflect the attributes that real world object has and what operation it can do (functions). If we take a simple example of Car (as Ponick used), to make it follow good style of OOP, we may make following classs,

class Car {
private $name, $model, $manufacturer, $class, $year;
public function start()
{
}
public function accelerate() 
{
}
public function brake()
{
}
public function stop()
{
}
}

Thus it will reflect real operations and attributes. It will have accessor method( get methods ) & mutator methods (set methods). I hope, this example gave you an basic idea about OOP paradigm

Follow me @ksg91 | My Blog: http://ksg91.com | Nokia Blog: http://NokiaTips.in

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close