oop

php5 extend main class and use statics

why I can't do like this? <?php class core { public static $db; function __construct() { $this->db = new mysql('host', 'user', 'pw', 'db'); } } class stat extends core { public static function log() { core::$db->query("insert into mytable values(now())"); } } // do something stat::log(); ?> ...

How do you manage database connections in php?

So recently I've really started to use php actively, and I need some insights on different ways to use database connections. At first I just used the simple mysql_connect(): <?php $connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error()); mysql_select_db(DB_DB, $connection); ?> After a while I created a da...

JavaScript: How to create a new instance of a class without using the new keyword?

I think the following code will make the question clear. // My class var Class = function() { console.log("Constructor"); }; Class.prototype = { method: function() { console.log("Method");} } // Creating an instance with new var object1 = new Class(); object1.method(); console.log("New returned", object1); // How to write a factory wh...

A very basic question about the way GUI integrated with the Logic classes

hi, assume i have a huge input form, which of course representing classes. i need this input to be loaded into the class's instances. this input obviously contains (some very complicated validation) checks, obviously the logic layer contains those input validation already. the question is what am i doing with gui. should i just, in a v...

Why is method overloading not defined for different return types?

In Scala, you can overload a method by having methods that share a common name, but which either have different arities or different parameter types. I was wondering why this wasn't also extended to the return type of a method? Consider the following code: class C { def m: Int = 42 def m: String = "forty two" } val c = new C val...

changed object state after behavior that used the state

I want to give my previous question a second chance since I think I have chosen a bad example. The question is how I should deal with situations where an object still can change after I have used it to do something and the new state is relevant for what is being done. Example in pseudo-code: class Book method 'addChapter': adds a ...

is it allowed to create a php class inside another class

Hello, I was wondering if it is allowed to create a class inside another class. It's actually the database class or, do I have to create it outside and then pass it in threw the constructor? but then I have created it without knowing if I would need it example: class some{ if(.....){ include SITE_ROOT . 'applicatie/' . 'db.class.p...

Elegantly designing a method with 2 overloads, one accepts an object the other doesn't

I need to design a method that can potentially take as a parameter an object, if it doesn't then the method has to create a new object by itself. Is this a good way to do it? public void Method1(int companyId, int userId, int clientId) { Method1(null, companyId, userId, clientId); } public void Method1(SpecialObject o, int company...

Designing classes in C#

Hello guys. I know this must be a common question, but take a look: Here i have a test class: public class EmployeeClass { private int _id; private string _name; private double _salary; public int id { get{...} set{...} } public string name { get{...} set{...} } //and so on } The question is: for me, it doesnt make sense to have pu...

Unit testing a method called during initialization?

I have a class like the following: class Positive(object): def __init__(self, item): self._validate_item(item) self.item = item def _validate_item(self, item): if item <= 0: raise ValueError("item should be positive.") I'd like to write a unit test for _validate_item(), like the following: ...

Using double negatives to test for conditions

To validate user inputs, I am unsure of which of two alternatives is better 1 isNull( Object input ) 2 notNull( Object input ) apache's commons lang library chose #2 but I am uncomfortable with double negatives. What do you say ? ...

How to set ID in Object Oriented Code.

I'm a bit confused when it comes to Object oriented programming for say a 3 tier application. Here is just a small example of what I am trying to do (I will shorten the database design to make it simple). Assume I am making a ticket help desk system. A ticket has a description, a responsible person, a due date, and of course an ID (un...

Proper way to pass along many variables in an OOP design (PHP)

How would I properly pass along data to a class being referenced in this structure: -Head Index - PageBuilder - PageSection -Body -Foot I want to send lots of data to head, for example, but I would like to avoid this: new PageSection('head','how to cook',$keywords,$...

Career Killer? Nhibernate, OOP, Design Patterns, Domain Driven Design, Test Driven Development, IoC, MVC

I have a fairly slick approach to doing C# development using the above tools/methodologies. Specifically i follow the "Jeffrey Palermo Agile Bootcamp" onion architecture. I feel like I'm a strong developer/architect using these tools and that these make me more productive and help make more maintainable code. I think these are all imp...

Simplifying Flex/AS3 code

Hello! I was programming in php for a while but it was all procedural-oriented. Now I have a project in Flex 3 and I made a simple script which animates (moves) few objects but I think that I am missing the point of object-oriented programming here because I am repeating some stuff over and over... Maybe it is mixed together with all of ...

recreate instance in base class

Hi all, I was wondering if it is possible to change the type of an instance of a derived class in it's base class to another derived class from the same base . following I will try to explain it in a code . public class ValueTypeClass { private string _Note; private String _Name; private node...

How to design collection in CSharp

I have a class "MySet" class MySet { ....... } This class will declare a reference to another type (i.e) class MySubSet { .... } The purpose of the type "MySubset" is to supply "subset id" and a collection of integers to the type "MySet". Which one of the followings is the correct implementation (1) class MySet...

Creating a collection of all classes that inherit from IBlahblah

Using reflection (i'm guessing?), is it possible to create a method that will return a collection of all objects that inherit from an interface named IBlahblah? public interface IBlahblah; ...

Events in an Inversion of Control (Dependency Inversion) system go which way?

Up or Down? I'm a very visual person. I'm thinking of my application as a hierarchy, where the top is the root and the bottom is a leaf. I'm also under the understanding that IoC containers are ignorant of their contained objects' responsibilities/functions. Instead, the contained objects know about their container, i.e. "context", via...

How to Properly Enforce Correct Usage of a Class Method?

Our current ORM solution uses Data Mappers to represent tables / views in the database which then return a Collection object that can be used to iterate through the retrieved records as Model objects. Between the Data Mapper and Model layers is a Repository layer that handles domain requests to the data mappers and returns the correspond...