Jump to content

[SOLVED] Encapsulation, polymorphism, interfaces, wtf?


matthewhaworth

Recommended Posts

As a side note, can you call a function before you've defined it Huh  Or does PHP put functions higher in processing precedence or something?

 

<?php

doSomething();

function doSomething() {
   print "hey mom, you suck!";
}
?>

 

That works, if that's what you were asking :)

Link to comment
Share on other sites

And if this is an abstract factory, then why is the class not abstract, because I mean, you can call the function "getShipping" from the class AND initiate the class, it's not abstract, to me the following example is just a regular factory pattern.  And why's it called a 'factory' pattern?

 


class shipping{
public static getShipping($company){
switch($company){
case 'fedex':
return new fedex()
break;
case 'usps':
return new usps();
break;
}
}
abstract function calculateCost();
protected function ourMarkup(){ return 2;}
}

class fedex extends shipping{
public function calculateCost(){
return 4 + $this->ourMarkup();
}
}

class usps extends shipping{
public function calculateCost(){
return 3 + $this->ourMarkup();
}
}

 

 

 

I'm sorry, can someone answer this question?

Link to comment
Share on other sites

Factory means it's an object which creates and returns objects.

 

Abstract factory means you have 3 things:

1. The Abstract Factory (in our case, an interface or abstract class)

2. ConcreteFactory (in the case above, Shipping)

3. ConcreteProducts (in the case above, USPS, FedEx, DHL, UPS..)

 

Really, this page explains all of it: http://en.wikipedia.org/wiki/Abstract_factory_pattern

 

Link to comment
Share on other sites

Factory is something that creates generally same type of things but let's say different instances, right?

A factory may create nike shoes but also adidas shoes. Every shoe implements the same interface. They wrap our feet and things like that.

 

Let's sat you have a database object which connects to the database. You create it by:

 

$db = new Datatabase();

 

Thing is when you pass from mysql to mssql, you should change all your syntax right? mysql_query must be replaced with mssql_query and it's a really annoying bussiness anyway. Let's say that you have used the factory method.

When creating Database() you send it a database type to your Database object. It has a driver which executes your sql queries. So your database creates another object to execute your sql queries. This newly created drivers are specified to the database type, mysql has different  syntax mssql has different types but they have the same method names as they implement the same interface. So whatever be your database driver you can call query() function.

 

So you factory creates another object that object may create another objects and this goes like that. That's why it's called 'factory', it creates instances of different object with nearly the same functionality.

 

I hope I was able to explain that clearly and without mistakes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.