SitePoint Sponsor |
|
User Tag List
Results 1 to 25 of 42
-
May 16, 2008, 09:27 #1
- Join Date
- Nov 2005
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Why is an OOP class called a 'class'?
Why is an OOP class called a 'class'?
-
May 16, 2008, 09:33 #2
- Join Date
- May 2005
- Location
- Singapore
- Posts
- 1,261
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I don't really know the answer to that, but I'm assuming because it's a sort of classification? Why do you want to know anyways?
Winners Respond. Losers React.
Singapore Web Designer
-
May 16, 2008, 09:40 #3
- Join Date
- Nov 2005
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
'anyway' is the word.
Why? I don't like to work with things that mean nothing to me.
-
May 16, 2008, 09:44 #4
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Think of it as classification - a variable has a certain classification, therefore does certain things.
Personally I would have called it "object" or something - and called interfaces 'class'.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
May 16, 2008, 09:53 #5
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The name class hints at the difference between the abstract concept and a concrete instance of that concept. In OOP, a class is never used directly. Instead, you create instances (Objects), which belong to a class. So you can think of class as a generic type, while the object is a concrete example of that type. Sort of like the word "horse" identifies all horses in the world, past and present, while Jolly Jumper is a concrete instance of said type.
-
May 16, 2008, 10:04 #6
- Join Date
- Nov 2005
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've got a lot of work ahead of me! I am using a reasonably complex OOP application with very little commenting. At the moment, it's still quite opaque to em!
-
May 16, 2008, 10:10 #7
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
How much experience do you have with OOP?
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
May 16, 2008, 11:35 #8
- Join Date
- Feb 2007
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 16, 2008, 12:40 #9
- Join Date
- Nov 2005
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
"me" is the word.
You're right of course.
-
May 16, 2008, 12:45 #10
- Join Date
- Nov 2005
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Oop
How much experience do you have with OOP?
Very little. This guestbook application is my first introduction to it.
I've been able to introduce some tests to exclude rubbish. I have great difficulty in following the flow of execution. The code is essentially without comments.
-
May 16, 2008, 13:07 #11
- Join Date
- Nov 2005
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What does it do?
If I have
include("config.php");
$this->indexsize = $indexsize;
$this->entriesperpage = $entriesperpage;
Does this make the values of $indexsize and $entriesperpage from config.php available in all instances of the current class? Or only within the current instance (object)?
-
May 16, 2008, 13:25 #12
- Join Date
- Feb 2007
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
$this is used to refer to a particular instance of a class. It makes no sense to use $this outside the scope of a class method.
PHP Code:class Product {
var $name;
function Product($name) {
$this->name = $name;
}
function getName() {
return $this->name;
}
}
$a = new Product('A');
$b = new Product('B');
echo $a->getName() . ' ' . $b->getName();
That's PHP4-ish style code. Static class variables (i.e., values that are shared across instances of the same class) aren't available there, but they are available in PHP5.
I'd suggest reading the manual and searching for some tutorials on the basics of OOP in PHP5.
-
May 16, 2008, 13:38 #13
- Join Date
- Nov 2005
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
include("config.php");
$this->indexsize = $indexsize;
$this->entriesperpage = $entriesperpage;
is within a function (method?) of a certain class. Must it not be avaiiable within all instances (objects?) of that class?
-
May 16, 2008, 13:58 #14
- Join Date
- Feb 2007
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yep, a function in a class is known as a method in OO parlance.
Anything accessed through the $this variable is bound to a particular instance of a class, which is exactly what the special $this variable signifies.
Variables shared across instances of a class are known as static class variables. PHP4 did not support these, but PHP5 does.
PHP Code:class Product {
private $name;
private static $textTemplate = 'this is product %s';
public function __construct($name) {
$this->name = $name;
}
public function getText() {
return sprintf(self::$textTemplate, $this->name);
}
}
$a = new Product('A');
$b = new Product('B');
echo $a->getText() . ', ' . $b->getText();
That will only work in PHP5 obviously.
-
May 16, 2008, 14:13 #15
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 16, 2008, 15:21 #16
- Join Date
- Nov 2005
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It makes no sense to use $this outside the scope of a class method.
But if it is used within a method belonging to a certain class, must it not refer to all instances of that class?
-
May 16, 2008, 15:38 #17
- Join Date
- Feb 2007
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hmm, umm, let me think about that a minute... uhhm, no. Nope. NOOO!
Think of it this way...
PHP Code:class Person {
private $confusedAboutOOP;
public function __construct($confused) {
$this->confusedAboutOOP = $confused;
}
public function talkAboutOOP() {
if ($this->confused) {
echo 'Wait, wait, wait... $this is shared across instances, right?';
} else {
echo 'no. Actually... no. NOOOOO!';
}
}
}
$bgsomers = new Person(true);
$cuberoot = new Person(false);
$bgsomers->talkAboutOOP();
$cuberoot->talkAboutOOP();
A class describes the properties something has in abstract terms, but an instance describes them concretely. For example, every person has a height attribute, but the height of each individual will vary.
-
May 16, 2008, 15:39 #18
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Ok, time for some OOP basics.
- A class is practically a set of variables and methods.
- An object is a variable which uses the instructions (vars and methods) laid out by the class.
- You use the "->" operator to access variables and methods from an object:
- If you are outside the object, use the variable name. For example, if the variable holding an object is $a, use $a->function().
- If you are in the class code block, the object doesn't exist yet. So you use '$this' to tell the object that it is refering to itself, no matter it's variable name - however, it does not affect the other objects using that class, unless they run that function.
- Static variables are those which are the same for all instantiations of a class. These use the '::' operator to access them. Instead of '$this', it is called 'self', referring to all objects in that class. Outside of that class, it's refered to by the class name, e.g. class_name::$var = 'hello'. Note that you keep the dollar sign for the variable.
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
May 16, 2008, 15:41 #19
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Cuberoot - a typo, I hope.
Should be:
PHP Code:class Person {
private $confusedAboutOOP;
public function __construct($confused) {
$this->confusedAboutOOP = $confused;
}
public function talkAboutOOP() {
if ($this->confusedAboutOOP) {
echo 'Wait, wait, wait... $this is shared across instances, right?';
} else {
echo 'no. Actually... no. NOOOOO!';
}
}
}
$bgsomers = new Person(true);
$cuberoot = new Person(false);
$bgsomers->talkAboutOOP();
$cuberoot->talkAboutOOP();
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
May 16, 2008, 15:46 #20
- Join Date
- Feb 2007
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ha! Good save.
-
May 17, 2008, 05:29 #21
- Join Date
- Nov 2005
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
# You use the "->" operator to access variables and methods from an object:
* If you are outside the object, use the variable name. For example, if the variable holding an object is $a, use $a->function().
* If you are in the class code block, the object doesn't exist yet. So you use '$this' to tell the object that it is refering to itself, no matter it's variable name - however, it does not affect the other objects using that class, unless they run that function.
# Static variables are those which are the same for all instantiations of a class. These use the '::' operator to access them. Instead of '$this', it is called 'self', referring to all objects in that class. Outside of that class, it's refered to by the class name, e.g. class_name::$var = 'hello'. Note that you keep the dollar sign for the variable.
Thanks arkinstall -- clearest explanation I've seen to date
-
May 17, 2008, 09:08 #22
- Join Date
- Feb 2008
- Posts
- 109
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 17, 2008, 09:46 #23
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 18, 2008, 08:55 #24
- Join Date
- Feb 2007
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What do you think about the following transaction script approach from "PHP5 Objects, Patterns, and Practice" --
PHP Code:abstract class DbLogic {
private static $db;
private static $stmts = array();
public function __construct(PDO $db) {
self::setDbConnection($db);
}
public setDbConnection(PDO $db) {
self::$db = $db;
}
protected function prep($sql) {
if (self::$stmts[$sql]) {
return self::$stmts[$sql];
}
self::$stmts[$sql] = self::$db->prepare($sql);
return self::$stmts[$sql];
}
protected function exec($sql, $data = null) {
$stmt = $this->prep($sql);
$stmt->execute($data);
// throw some exceptions here...
return $stmt;
}
}
interface IThingLogic {
public function addThing($data);
public function getThings();
}
class ThingLogic extends DbLogic implements IThingLogic {
public function addThing($data) {
return $this->exec('INSERT INTO things (name) VALUES (:name)', $data);
}
public function getThings() {
return $this->exec('SELECT * FROM things');
}
}
Does this qualify as a poor design choice? If so, what makes it so? What do you lose with this approach?
-
May 18, 2008, 09:26 #25
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Presumably, there would be a multitude of classes derived from DbLogic. Some of these might utilise the same queries, so since preparing a statement costs some calculations, there would be a slight performance gain by sharing it across classes.
I think so, for a number of reasons. First off, this is premature optimisation. Sure, there is some waisted clock-cycles, but those are most likely a drop in the ocean anyway. One shouldn't employ optimisation techniques before there is a proven bottleneck.
Secondly, this is using inheritance for something that is more aptly solved with composition. A much better solution would be to create a query-cache wrapper as a decorator to the database-connection. That would make the ThingLogic's implementation simpler and would make it easier to replace (Or introduce in the first place) the caching-behaviour, should that be needed.
Bookmarks