|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Jul 2003
Location: Kortrijk, Belgium, Europe, the world
Posts: 203
|
another OOP question
Hello,
i've recently started to use OOP. I read the chapter in The PHP Anthology and I came up the following classes for handeling newsitems(3 languages, dutch, french and english): Some translations: feit = fact titel = title file: newsItem.php PHP Code:
PHP Code:
And second, how can I implement a function to insert a new newsItem in the database? tia |
|
|
|
|
|
#2 |
|
Non-Member
![]() Join Date: Sep 2004
Location: United Kingdom (Come)
Posts: 80
|
Yo
The second script example look okay, though not the first I'm afraid ![]() For example, your class supports 3 languages, but what happens if you need a Norwegian translation ? Well, you'd have to change the class. This isn't the best approach in two ways; 1) You have all translations within the same class, I'd have a base class, and extend each translation, and 2) Even though I don't have a solution for you, I'm getting a feeling from reading another related thread that using OO as a content container isn't ideal. Anyway, onto your second question, you could use a Gateway pattern for this, for example have a look at this script posted in this thread for an example; http://www.sitepoint.com/forums/showthread.php?t=186476 Hope this helps you ![]() |
|
|
|
|
|
#3 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Aug 2004
Location: California
Posts: 1,672
|
My personal opinion is that classes like your newsItem are a crazy use of OO in PHP. Others will disagree, but I think associative arrays are better than OO in mamy cases. Simpler would be just one class:
PHP Code:
|
|
|
|
|
|
#4 | |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2001
Location: Bath, UK
Posts: 2,532
|
Quote:
Looks like the class is being used as a record rather than an object (in the OO sense), in which case an array is much better. Douglas |
|
|
|
|
|
|
#5 |
|
simple tester
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Sep 2003
Location: Glasgow
Posts: 1,685
|
There's nothing wrong with using a class as a container - see WACT use of dataspaces. If you were storing lots of different types of information in an array, you might think about an object instead. Rather than a messy multi-dimensional array structure each type can have its own property.
|
|
|
|
|
|
#6 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2001
Location: Bath, UK
Posts: 2,532
|
I wouldn't advocate an object or a multi-dimensional array here.
Why is data for all the languages being pulled from the database? If fetchNews() was called at fetchNews($lang) the returned data could be simplified to an array of 'nieuwsID', 'dt', 'feit' and 'titel'. This could be done in the SQL to save processing later: Code:
SELECT titel_$lang titel, feit_$lang feit PHP Code:
Last edited by DougBTX; Sep 16, 2004 at 06:01. |
|
|
|
|
|
#7 | |||
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Aug 2004
Location: California
Posts: 1,672
|
I agree with Douglas that this code could be simplified considerably. We'd need to know the bigger picture to know how to do that though.
Quote:
Quote:
Quote:
|
|||
|
|
|
|
|
#8 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Jul 2003
Location: Kortrijk, Belgium, Europe, the world
Posts: 203
|
All the languages are taken from the database at the same time because i use this for a content management system.
So, our site is can be viewed in three different languages, but if we want to insert a new newsitem, we have to provide it in all the different languages. |
|
|
|
|
|
#9 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Aug 2004
Location: California
Posts: 1,672
|
Yes, but you only view the whole site in one language at a time. Therefore you can have a single global setting for the language. Then when you fetch an article you will only get the id, date and content for the current language. It greatly simplifies your other code because it only needs to look for $row['title'].
|
|
|
|
|
|
#10 |
|
Non-Member
![]() Join Date: Sep 2004
Location: United Kingdom (Come)
Posts: 80
|
Yo
Off topic slightly but has anyone thought about Web Services yet ? |
|
|
|
|
|
#11 | |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2001
Location: Bath, UK
Posts: 2,532
|
Quote:
Too off topic for me! Can you give me some hints as to what you are talking about?? Do you mean "perhaps he is serving a web service which must deliver all the content in three languages for the clients to make the lang choice"? Douglas |
|
|
|
|
|
|
#12 |
|
SitePoint Enthusiast
![]() Join Date: Jun 2004
Location: Stillwater, MN
Posts: 97
|
At the very least stick them in an array and use something like:
PHP Code:
|
|
|
|
|
|
#13 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Dec 2003
Location: oz
Posts: 822
|
For multiple languages, you might like to try out the strategy pattern. It would make adding more languages much easier, as well as improve code readability and ease maintainance time
PHP Code:
PHP Code:
PHP Code:
|
|
|
|
|
|
#14 |
|
sitepoint wombat
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2000
Location: Perth Australia
Posts: 1,722
|
I think you really need to look at DougBTX's first post , there is no reason nor justification for pulling all the different language data from the DB if you only need the currently requested language, (of course you may need several languages at once but please note if so).
once the sql calls are dynamic then you can lose some of your method bloat as well (and easily add new languages) PHP Code:
|
|
|
|
|
|
#15 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Dec 2003
Location: oz
Posts: 822
|
That's trivial to solve. You do it exactly the same way - create an abstract method to subclass and put any language specific code there, and call that method from the super class.
Just have another abstract method in classes that subclass News, and put in something like PHP Code:
PHP Code:
|
|
|
|
|
|
#16 |
|
sitepoint wombat
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2000
Location: Perth Australia
Posts: 1,722
|
I know its simple to solve , (I dont think your example is that solution since you still end up having to create new classes for each language where a simple bit of variable substitution would suffice ($this->lang)) the point is is that is where you should be building/starting from , not bolting on later ?
like the extended engishNews and frenchNews classes , now pointless unless there is something we have not been told about that would require seperate or extended classes for handling french news any differently from other languages. I am not sure how far Avido wants to go with the translation thing , if its only a few languages this `all in one db` approach may work but if the list grows past a few , then seperate tables, possibly even DB's for each language would be preferable , this would of course in itself solve the issues above , you then only need change the tablename (hopefully dynamically rather than by duplication) |
|
|
|
|
|
#17 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Jul 2003
Location: Kortrijk, Belgium, Europe, the world
Posts: 203
|
Ok, some explanation first
the development of the site has a history of about 2 years(I've use most of the time creating sites for customers). It's untill recently that I've started to try thinking in an object oriented way. So, you can see on the following image how a newsitem is displayed.(is in dutch because the translations aren't ready yet) ![]() You have an image, a title and a newsfact. The dates are used to create the navigation(31 days, 12 months and the available years). When i've created this a couple of months ago, i used functions to get the desired result. Now, if you know the above fact, what should be better? Leave it like this(because it works ) and use an array to store the data in, or place this functionality(to get the navigation) inside the class(or a differen class)?Btw, thx for the replies allready made. |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 05:28.









) and use an array to store the data in, or place this functionality(to get the navigation) inside the class(or a differen class)?


Linear Mode
