Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Interestingly, my review is going to be all about control.

Not Enough - Public Property

Just like in the real world, public property can be used by anyone. If your car was public property anyone could walk into your driveway and do whatever they liked with it. When you woke up you could not expect to $this->car->drive() (What if someone had already taken it and replaced it with null?). If they wanted to they could place a house brick in your driveway pretending that it is your car (good luck driving that).

Public properties remove the encapsulation of state from an object. I see virtually no place for them in OO. Protected and Private properties on the other hand are set through using the public interface (the public methods) of an object. Importantly the public interface is testable, so you can ensure that you will not end up with a brick in your driveway unless you really want that.

Too Much - Inversion of Control

Your code will benefit from an Inversion of Conrtol (IoC). You can read about IoC on the web. There is a good answer on stackoverflow with code here here.

With IoC and the protected properties your code becomes:

class Page {
 protected $title;
 protected $db;
 public function __construct($db, $title) {
 $this->db = $db;
 $this->title = $title;
 }
}

Your code is no longer dependent on the specific database setup to function correctly. Your page will use any database connection that it is given. This has big benefits when it comes to unit testing. See Miško Hevery - How to Think About the "new" Operator with Respect to Unit Testing.

I have another answer that covers similar things with an extra part on injecting interfaces here.

Interestingly, my review is going to be all about control.

Not Enough - Public Property

Just like in the real world, public property can be used by anyone. If your car was public property anyone could walk into your driveway and do whatever they liked with it. When you woke up you could not expect to $this->car->drive() (What if someone had already taken it and replaced it with null?). If they wanted to they could place a house brick in your driveway pretending that it is your car (good luck driving that).

Public properties remove the encapsulation of state from an object. I see virtually no place for them in OO. Protected and Private properties on the other hand are set through using the public interface (the public methods) of an object. Importantly the public interface is testable, so you can ensure that you will not end up with a brick in your driveway unless you really want that.

Too Much - Inversion of Control

Your code will benefit from an Inversion of Conrtol (IoC). You can read about IoC on the web. There is a good answer on stackoverflow with code here.

With IoC and the protected properties your code becomes:

class Page {
 protected $title;
 protected $db;
 public function __construct($db, $title) {
 $this->db = $db;
 $this->title = $title;
 }
}

Your code is no longer dependent on the specific database setup to function correctly. Your page will use any database connection that it is given. This has big benefits when it comes to unit testing. See Miško Hevery - How to Think About the "new" Operator with Respect to Unit Testing.

I have another answer that covers similar things with an extra part on injecting interfaces here.

Interestingly, my review is going to be all about control.

Not Enough - Public Property

Just like in the real world, public property can be used by anyone. If your car was public property anyone could walk into your driveway and do whatever they liked with it. When you woke up you could not expect to $this->car->drive() (What if someone had already taken it and replaced it with null?). If they wanted to they could place a house brick in your driveway pretending that it is your car (good luck driving that).

Public properties remove the encapsulation of state from an object. I see virtually no place for them in OO. Protected and Private properties on the other hand are set through using the public interface (the public methods) of an object. Importantly the public interface is testable, so you can ensure that you will not end up with a brick in your driveway unless you really want that.

Too Much - Inversion of Control

Your code will benefit from an Inversion of Conrtol (IoC). You can read about IoC on the web. There is a good answer on stackoverflow with code here.

With IoC and the protected properties your code becomes:

class Page {
 protected $title;
 protected $db;
 public function __construct($db, $title) {
 $this->db = $db;
 $this->title = $title;
 }
}

Your code is no longer dependent on the specific database setup to function correctly. Your page will use any database connection that it is given. This has big benefits when it comes to unit testing. See Miško Hevery - How to Think About the "new" Operator with Respect to Unit Testing.

I have another answer that covers similar things with an extra part on injecting interfaces here.

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Interestingly, my review is going to be all about control.

Not Enough - Public Property

Just like in the real world, public property can be used by anyone. If your car was public property anyone could walk into your driveway and do whatever they liked with it. When you woke up you could not expect to $this->car->drive() (What if someone had already taken it and replaced it with null?). If they wanted to they could place a house brick in your driveway pretending that it is your car (good luck driving that).

Public properties remove the encapsulation of state from an object. I see virtually no place for them in OO. Protected and Private properties on the other hand are set through using the public interface (the public methods) of an object. Importantly the public interface is testable, so you can ensure that you will not end up with a brick in your driveway unless you really want that.

Too Much - Inversion of Control

Your code will benefit from an Inversion of Conrtol (IoC). You can read about IoC on the web. There is a good answer on stackoverflow with code here.

With IoC and the protected properties your code becomes:

class Page {
 protected $title;
 protected $db;
 public function __construct($db, $title) {
 $this->db = $db;
 $this->title = $title;
 }
}

Your code is no longer dependent on the specific database setup to function correctly. Your page will use any database connection that it is given. This has big benefits when it comes to unit testing. See Miško Hevery - How to Think About the "new" Operator with Respect to Unit Testing.

I have another answer that covers similar things with an extra part on injecting interfaces here here.

Interestingly, my review is going to be all about control.

Not Enough - Public Property

Just like in the real world, public property can be used by anyone. If your car was public property anyone could walk into your driveway and do whatever they liked with it. When you woke up you could not expect to $this->car->drive() (What if someone had already taken it and replaced it with null?). If they wanted to they could place a house brick in your driveway pretending that it is your car (good luck driving that).

Public properties remove the encapsulation of state from an object. I see virtually no place for them in OO. Protected and Private properties on the other hand are set through using the public interface (the public methods) of an object. Importantly the public interface is testable, so you can ensure that you will not end up with a brick in your driveway unless you really want that.

Too Much - Inversion of Control

Your code will benefit from an Inversion of Conrtol (IoC). You can read about IoC on the web. There is a good answer on stackoverflow with code here.

With IoC and the protected properties your code becomes:

class Page {
 protected $title;
 protected $db;
 public function __construct($db, $title) {
 $this->db = $db;
 $this->title = $title;
 }
}

Your code is no longer dependent on the specific database setup to function correctly. Your page will use any database connection that it is given. This has big benefits when it comes to unit testing. See Miško Hevery - How to Think About the "new" Operator with Respect to Unit Testing.

I have another answer that covers similar things with an extra part on injecting interfaces here.

Interestingly, my review is going to be all about control.

Not Enough - Public Property

Just like in the real world, public property can be used by anyone. If your car was public property anyone could walk into your driveway and do whatever they liked with it. When you woke up you could not expect to $this->car->drive() (What if someone had already taken it and replaced it with null?). If they wanted to they could place a house brick in your driveway pretending that it is your car (good luck driving that).

Public properties remove the encapsulation of state from an object. I see virtually no place for them in OO. Protected and Private properties on the other hand are set through using the public interface (the public methods) of an object. Importantly the public interface is testable, so you can ensure that you will not end up with a brick in your driveway unless you really want that.

Too Much - Inversion of Control

Your code will benefit from an Inversion of Conrtol (IoC). You can read about IoC on the web. There is a good answer on stackoverflow with code here.

With IoC and the protected properties your code becomes:

class Page {
 protected $title;
 protected $db;
 public function __construct($db, $title) {
 $this->db = $db;
 $this->title = $title;
 }
}

Your code is no longer dependent on the specific database setup to function correctly. Your page will use any database connection that it is given. This has big benefits when it comes to unit testing. See Miško Hevery - How to Think About the "new" Operator with Respect to Unit Testing.

I have another answer that covers similar things with an extra part on injecting interfaces here.

Source Link
Paul
  • 4k
  • 2
  • 22
  • 38

Interestingly, my review is going to be all about control.

Not Enough - Public Property

Just like in the real world, public property can be used by anyone. If your car was public property anyone could walk into your driveway and do whatever they liked with it. When you woke up you could not expect to $this->car->drive() (What if someone had already taken it and replaced it with null?). If they wanted to they could place a house brick in your driveway pretending that it is your car (good luck driving that).

Public properties remove the encapsulation of state from an object. I see virtually no place for them in OO. Protected and Private properties on the other hand are set through using the public interface (the public methods) of an object. Importantly the public interface is testable, so you can ensure that you will not end up with a brick in your driveway unless you really want that.

Too Much - Inversion of Control

Your code will benefit from an Inversion of Conrtol (IoC). You can read about IoC on the web. There is a good answer on stackoverflow with code here.

With IoC and the protected properties your code becomes:

class Page {
 protected $title;
 protected $db;
 public function __construct($db, $title) {
 $this->db = $db;
 $this->title = $title;
 }
}

Your code is no longer dependent on the specific database setup to function correctly. Your page will use any database connection that it is given. This has big benefits when it comes to unit testing. See Miško Hevery - How to Think About the "new" Operator with Respect to Unit Testing.

I have another answer that covers similar things with an extra part on injecting interfaces here.

lang-php

AltStyle によって変換されたページ (->オリジナル) /