oop

Call parent constructor before child constructor in PHP

Hey all, I was wondering if its possible to call the parents __construct(), before the child's __construct() with inheritance in PHP. Example: class Tag { __construct() { // Called first. } } class Form extends Tag { __construct() { // Called second. } } new Form(); Ideally, I would be able t...

Have a child extend an already initialized parent in PHP

Hey all, I've been having a hard time coming up a solution with this one. I'm hoping you all can help me out. Best described with an example: class Parent { public $nationality; function __construct($nationality) { $this->nationality = $nationality } } class Child extends Parent { function __construct() {...

MySQLi as an static class

I have the following code that is able to create a class that has a single static instance of the database object, and two static functions for rows and columns. <?php class Database{ private static $instance; private function __construct() {} private function __clone(){} public static function call(){ if(!iss...

Why aren't (C++) virtual destructors enforced for a base class

Destructors aren't virtual by default to not hurt when its not needed, which is fine. But in case of a base class derived class scenario, is there any use case for not having a virtual destructor? If not could it be possible (does it make sense) for the compiler to complain if a class derives from a base class which has a public non vir...

How to get error messages from model

Say you have a User model. The controller is attempting to create a new User. Should the controller check that the username is valid, and the password is long enough, and the first and last name are filled out, etc? Or should you pass all that data straight to the User model via a Create method? The Create method would then return a tru...

Optimal extended class naming convention?

Which of the following naming conventions is most used/accepted, or which is most optimal for most projects? I'm having a hard time deciding. Base class: \Model Sub class: \Model\User Base class: \Model\Model Sub class: \Model\User Base class: \Model\Base sub class: \Model\User So in other words, should I put the base class in the s...

Why? Implicit initialization from uninitialized parent class members

I had a nasty experience with C++ initialization, and I'm trying to see whether there is a real-world example which justifies no warnings from the compiler. The following code compiles correctly, but foo and bar get initialized with uninit values (I assume from the uninitialized parent class). The compilers, both g++ and VS, don't emit ...

When should I go for an Interface and when for an abstract class?

Possible Duplicate: Interface vs Base class I was just thinking about when should some one go for Interface rather than an abstract class. I know interface is useful for multiple inheritance but apart from that what other advantages it provides over abstract class? ...

Design patterns for functional-oo hybrid languages?

Is there already any collection of best practices for languages like Scala? I've found a work on design patterns for functional languages here. There's GoF design patterns for oo languages. But are there any patterns for functional-oo hybrids? All I've seen is this list. Does anyone know any more? ...

Cohesion & Coupling

Anyone point out me some good example for cohesion and coupling. Increasing cohesion/coupling will lead to good software design? ...

How should I pass a MySQL database link to a class in PHP?

Hey all, I'm trying to make a small CMS for fun but I'm getting caught up on some semantics with OOP. I have a basic database class and then I have an administrator class. The admin class has to make database queries, so what I've been doing is having the admin class extend the db class and then passing the link to the db resource via c...

Is this case an overuse of abstraction?

We are using a library provided by someone else. Of late due to changes in that library our project has 500 errors. When migrating to the new library we found that only 15 APIs are failing and 500 errors are repetitive (multiples occurrences of those 15 errors, as same calls are used at so many times). So, for migration I proposed creat...

Is it good practice to use domain objects as keys?

Is is good practice to use domain objects as keys for maps (or "get" methods), or is it better to just use the id of the domain object? It's simpler to explain with an example. Let's say I have Person class, a Club class, and a Membership class (that connects the other two). I.e., public class Person { private int id; // primary ke...

How to handle state transitions and yet replace "if" statements with polymorphic types?

Recently I was listening to a tech talk on clean coding. The speaker was a test engineer, who emphasized on avoiding the "if" statements in the code and use polymorphism as much as possible. Also he advocated against global states. I quite agree with him, yet i need a clarification on replacing the global state and "if" statement using ...

How to filter off of a country code?

Okay, here's what I'm attempting. I have a drop down list with all of the countries in the world. The user selects one, and that value is passed to a case statement, if it matches the case, an email is sent. I only have four different recipents, but I can have upwards to dozens of countries to match with each one of the four emails. F...

Constructing Child Objects in PHP

I have an abstract parent class Item, from which different types of items extend: ItemTypeA, ItemTypeB, and ItemTypeC. From my database result, I have an array: array( 'item_name' => 'This is the item name', 'item_type' => 'ItemTypeA' ); In PHP, what's the best way to create the item? Should I do something similar to the following? ...

Create a new copy of object itself with some new properties

Sometimes it's difficult to explain in human language what you want to do in programming, but I will try... Please explain to me, how can I implement the following logic. Suppose we have a template class: $obj1=new Tmpl($somevar1, $somevar2, ...); //we then add a new file to template //as we don't have any files yet, new object won't c...

Python: Elegant way to replace a given dictionary by child key somewhere in a tree?

Is there some kind of enumeration library or method I should use or should I write from scratch with recursion? I'm parsing a JSON tree into an object tree as it happens, and I'd like to replace some nodes with other kinds of objects. E.g: db = {'bigBang' : {'stars': {'planets': {}, 'is_list':true ...

Default value of member variables

What is the default value of class member variables in PHP? Why do I often see: public static $variable = null; Wouldn't it be enough: public static $variable; ...

consolidating class members in PHP

I recently came across an article by Matthew Weier O'Phinney (ZF project lead) that contains sample code similar to this: class User { protected $_data = array( 'username' => null, 'email' => null, 'fullname' => '', 'role' => 'guest', ); /* ... */ } Notice how what would traditionall...