Vanilla 1.1.4 is a product of Lussumo. More Information: Documentation, Community Support.
Hi guys,
After a month or so away from it, I’m trying to jump back into learning some OOP. Now I remember what was making it so frustrating in the first place. Also, if you plan on learning with no previous experience from Java or C++ (like me!!) then I highly advise against trying Peter Lavin’s OO PHP book. There are a lot of words, but no meaning…it’s like I’m reading somebody babble on about how cool OO is instead of actually instructing anything.
At any rate, the frustrating part via an excerpt from that book…
var $filearray = array();
“Any variable declared at this level, namely inside the braces that enclose the class but outside any class function, is an instance variable or, as we might also refer to it, a data member. Instance variables are sometimes also referred to as properties.”
So, instance variable = data member, instance variable = properties and properties = data member…aka three terms that, to me, seem to have nothing to do with each other unless said so explicitly as it is in this case. Can we just pick one and go with it? Maybe me writing this down will help me to understand and remember that fact, I dunno. But it still seems like I can just say apples = oranges, apples = bananas and oranges = bananas and get away with it.
And does it seem like a good idea to introduce OOP with PHP4? Peter seems to think it is. I don’t want to see Photoshop CS3 tutorials telling me how something was done in Photoshop 7 and the differences between the versions. Who cares? Just give me how it works now. Doing a whole class in PHP4 and then going back through it and adjusting it for PHP5 is silly to me.
Ok, end rant. :)
If anyone has a suggestion on a really good OOP book, or some other source, I’d love to hear it.
Yeah, those terms make sense I think…though I don’t know how to apply them just yet. :)
Thanks for that, and your reference suggestion!
If it is OOP in general that you need help understanding here is how it was explained to me.
Object oriented code relies on us defining classes in our code. A class is pretty much just functions and variables that are separated into logical groups. Say we have two things we are working with, cookies and cars. A car has an engine (a variable) and we can start that engine (a function). A cookie has chocolate chips (a variable) and we can eat it (a function). We wouldn’t try to eat our engine so there is no need for our cookies to know about the engine.
Speaking of cookies, we don’t define an object in our code. What we actually do is define the properties of our object and what it will do. This was explained to me as being a cookie cutter. All of our objects will be cut from this class but will actually each be its own “instance”.
Let’s try writing a class for our car. First we need to describe what a car is. A car has an engine, a transmission, a certain number of doors, a color, and a number of miles it has been driven.
<?php
class Car {
public $engine;
public $transmission;
public $doors;
public $color;
public $mileage;
}
?>
Now we have variables that describe our car. Notice that our car has no engine, transmission, etc. We need to create an instance of our car in order to set these properties. We do this using the “new” keyword.
@<?php
class Car {
...
}
$myCar = new Car();
$myCar->color = ‘Red’;
echo “My car is “.$myCar->color.”<br/>”;
$yourCar = new Car();
$yourCar->color = ‘Black’;
echo “Your car is “.$yourCar->color;
?>@
So we have created two separate instances of car, $myCar and $yourCar, and have set the color property for each. They are both cars but they have their own distinct properties.
yeah, I’m getting along ok as far as the basic concepts and the point of OOP in general. It’s just the terms get me crossed up as to what refers to what in the code.
Hopefully I’m understanding this correctly now.
public $engine;
public $transmission;
public $doors;
public $color;
public $mileage;
are your static members, while:
$yourCar->color
would be an instance variable?
Actually these are all instance members. A static member would be preceded with the static keyword. Say we wanted all our cars to be Hondas. We could add a static field “Make”.
@
<?php
class Car {
public $engine;
...
public static $make = ‘Honda’;
Now if we go back and modify our first code we can add the following.
@
...
echo “Your car is “.$yourCar->color;
echo ‘My car is a ‘.$myCar->make().’<br/>’;
echo ‘Your car is a ‘.$yourCar->make().’<br/>’;
Car::$make = ‘Cheverolet’;
echo ‘My car is now a ‘.$myCar->make().’<br/>’;
echo ‘Your car is now a’.$yourCar->make().’<br/>’;
@
Notice that both cars are “Cheverolet” even though we only change the value of make for $myCar. This is because make is static. Static means that the same variable is used across all instances of Car. Also, static fields are accessed differently than instance fields. Rather than -> they are accessed with :: and they are also accessed directly from the class rather than the instance. This is why we have created a function called make() which returns the value of the static field.
Once you have got this down you will also need to take a look at the other modifiers. There is public, private, static, and const. These apply to functions/methods as well.
1 to 6 of 6