Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts

    oop style question

    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.

    http://gcgz.com/obj3.txt

    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.

  • #2
    New Coder
    Join Date
    Sep 2004
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    abstract class Parent
    {
        protected 
    $myArr = array('Your''Data''Items''here');
    }
    class 
    Child extends Parent
    {
        public function 
    __construct()
        {
            
    print_r($this->myArr);
        }

    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:
    PHP Code:
    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
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    mbannonb (01-30-2008)

  • #4
    New Coder
    Join Date
    Sep 2004
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •