oop

When should I use stdClass and when should I use an array in php5 oo code ??

In the middle of a period of big refactorings at work, I wish to introduce stdClass * as a way to return data from functions and I'm trying to find non-subjectives arguments to support my decision. Are there any situations when would it be best to use one instead of the other ?? What benefits would I get to use stdClass instead of arra...

Should you always Code To Interfaces In Java

I understand the principles of Coding to Interfaces - to decouple the implementation from the interface, and to allow implementations of the interface to be swapped in and out. Should I code to interfaces for every class I write or is that overkill? I don't want to double the number of source files in a project unless it's really worth...

Base class and derived class

Hi to all, I have a question, I have a base class and an another class which derived from the base class. Can we access derived class in the base class. Thanks in advance ...

how to create methods from arrays or hashes in perl6

I am trying to add new methods to an object dynamicaly. Following code works just fine: use SomeClass; my $obj = SomeClass.new; my $blah = 'ping'; my $coderef = method { say 'pong'; } $obj.^add_method($blah, $coderef); $obj.ping; this prints "pong" as expected, whereas the following will not work as expected: use SomeClass; my $...

Magento: select from database

hi there, I am working on my first module for magento version 1.3.2.3. I have created a simple table (not EAV, just a primary key and 2 columns) and some classes to access it, following Alan Storm's articles which helped me a lot, but I can't figure out how to make a simple select: Alan explains how to load with the primary key, but not ...

C++: Initialize a member pointer to null?

I have a class that looks like: class Foo { public: Foo(); virtual ~Foo(); private: Odp* bar; }; I wish to initialize bar to NULL. Is this the best way to do it? Foo::Foo() : bar(NULL) { } Also, is it necessary that the destructor is virtual? (If that is true, then must the constructor be virtual as well?) ...

c# SETTINGS architecture

I have several applications that need to read and write application settings. Since one of the apps is a legacy c++ app with hundreds of settings in the registry (and that isn't bound to change) and there are also some settings stored in a proprietary database format I'm wondering how the architecture could be setup to allow a single in...

PHP Class Database interaction (using PDO)

Hey guys, so I'm completely new to Object Oriented PHP -- I've read some tutorials, but I can't find anything that really goes into working with a database with PHP classes. I'm trying to make something simple -- a quick news post class. You get the post from the database, etc. However, I'm getting an error whenever I try to interact wi...

F# Friend function/class

Is it possible to implement friend function and friend class (as in c++) in F#? Update: Since there is no friend function/class in f#, and friend is not even a reserved keyword for future expansion, I'm wondering is there any problems with the friend mechanism in F# that make the developers to decide not to implement it? (such as in "p...

Class design: Passing values via Property(Set var) vs Function vs Constructor argument

This is a kind of open question. I always keep scratching my head when deciding between these. I can pass values to a class by: Passing an argument to class function: MyClass m = new MyClass(); m.DoSomething(arg); Passing argument when creating object: MyClass m = new MyClass(arg); m.DoSomething(); Setting the value using a differ...

What is the proper OOP way to configure class (dis)inheritance?

I cannot seem to google this one correctly... I have a class (Widget) that represents a database table from the Data Layer. The table holds 3 different types of records, where one uses only 5 columns, another uses 10 columns etc. Each record has a different set of validation and business rules that I want to control with Business Layer ...

Determining the representation-class for a data object based on its specialized type?

I tend to have a class to describe a general concept and subclasses to describe variances in that concept. For example, Polygon <|-- {Rectangle, Triangle, etc.}. However, I often find I have various representations of these hierarchies. For example, I want to keep the graphical representation (eg, a QPolygon), or the physical represen...

Is there a __get magic method equivalent for instantiating classes?

Looking to see if there is any way to implement the kind of functionality of the __get() magic method, but when trying to instantiate a new class. My goal is to have $foo = new Cat(); Ultimately result in the creation of a generic object, i.e. new Mammal('Cat'); Such that $foo would be an instance of the class Mammal, with the calle...

How to replace an instance in __init__() with a different object?

I am calling a constructor in ClassA and want to have the resulting object be of a different class (ClassB) if a certain condition is met. I've tried replacing the first argument to __init__() ('self' in the example below) within __init__() but it doesn't seem to do what I want. in main: import ClassA my_obj = ClassA.ClassA(500) # un...

How do I change out the underlying object inside a method call?

I thought all function and method arguments in Python were passed by reference, leading me to believe that the following code would work: class Insect: def status(self): print "i am a %s" % self class Caterpillar(Insect): def grow_up(self): self = Butterfly() # replace myself with a new object ...

Where should Exceptions be caught?

In designing class with multiple/nested has-a relationships, who should catch exceptions? I guess this depends on design but is there any rule of thumb? ...

Is it a good idea to use APIs of lower level package in classes of a higher level package?

By lower level package I mean a package a.b.c.d and by higher level package I mean a.b.c and I've used the terms in relation to each other. I'm not sure if there's a terminology for defining such levels. However, from object oriented perspective, I wanted to know if it is a good idea to call a.b.c.d.aClass.aMethod() from a.b.c.anotherCla...

Loose programming in high level languages, how, why and how much?

I'm writing my code in haXe. This is quite irrelevant to the question though, as long as you keep in mind that it's a high level language and compareable with Java, ActionScript, JavaScript, C#, etc. (I'm using pseudocode here). I'm going to work on a big project and am busy preparing now. For this question I'll create a small scenario ...

Get object information in a running process

I have a vb.net application which runs as a service. I also have another Windows application that serves as the service interface. In brief the service watches some folders for new files and imports them into various databases. In the service I have an class called 'importFile' containing basic properties such as 'FileName' and 'ImportS...

Creating a new Location object in javascript

Is it possible to create a new Location object in javascript? I have a url as a string and I would like to leverage what javascript already provides to gain access to the different parts of it. Here's an example of what I'm talking about (I know this doesn't work): var url = new window.location("http://www.example.com/some/path?name=va...