View Full Version : oop style question

01-18-2008, 09:36 PM
I have two separate sites that share the same code base.

I have a class called itemObj() and it contains the methods and properties for managing virtual item content on each site.

Here is what I was doing:

Setting the data in a file local to each site. http://gcgz.com/data1.txt
When I created an instance of itemObj, I would pass in the data from the local file to the class. http://gcgz.com/obj1.txt

This creates multiple places for maintenance as I use itemObj() on multiple pages and I'm sure can't be the best.

So I am having the original class have an empty construct(). http://gcgz.com/obj2.txt

And everywhere the itemObj class is included, I am also including a local itemLocalObj class that 'extends' itemObj. The itemLocalObj is what will be initialized and used everywhere.


While this seems a lot easier than the original solution I was using, I want to make sure I've got it right.

Any thoughts are appreciated.

01-18-2008, 10:26 PM
Another question.

I have a class for managing virtual pets and I have a class for managing virtual items.

They both require the same list of pet species in each class.

What is the best way to maintain a single pet species list that can be used by both classes?

01-19-2008, 07:10 AM
Um, ok I'm a little lost here, you will need to explain a little more exactly what you are looking at doing.
It looks like you have a parent class that has no real used functionality, and that the file in question is embedded directly into the object code.
To retain a list for multiple items its best to use something like an enum, which to my knowledge does not exist in php. So, we revert it to a more simplistic structure - an array or other applicable datastructure. If you define your own datastructure ensure you make it iterable, or searching/looping can become a pain.
Place this data in a parent or abstract parent class, this will be usable within all of the child classes. However, you must define the variables within the parent with a protected scope or provide the correct accessors / mutators for the data. I do not recommend making it public.

abstract class Parent
protected $myArr = array('Your', 'Data', 'Items', 'here');
class Child extends Parent
public function __construct()

Part of the problem is you have a partial class on a partial procedural script design. This can make things kind of confusing, I'd recommend choosing one or the other (and as an OOP developer, I'd go with object oriented myself).
Some of the other data you have are also objects, you have 'Animals', which is an abstract parent class, and animal types 'Shark', 'Merkaat?', 'Fish', which of course are classes (possibly parent classes) themselves.
OOP is one of those things you just need to practice at. Try doing some object modeling if you can (that is when you picture how the objects interact with each other), and things will become much easier!

I can tell you this offhand. Your code is ok, you *should* be declaring scopes for your object properties, where in your constructor every call with $this is also given a scope:

class myObj
protected $items_cat_arr;
protected $items_subcat_arr;

This is not however a requirement in php, just note that the default scope in PHP is public, not protected/internal like other oop languages.
Hope that helps a little, even though I'm not sure exactly what your problem is, lol

01-30-2008, 05:40 PM