Page 1 of 1

passing Class to function

Posted: Fri Oct 16, 2009 11:45 am
by dgkindy
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I have created a class called "Material" which is a collection of several properties.

Once I have imported all the data I might have 30 instances of Material.

One of the properties that I import is a date which can come in two formats: MM-DD-YY or YY-MM-DD

I have create the code to manipulate the date structure so that they all are formated the same however I have not tested it as I am not sure how to pass the information to the function/method. Also not sure but I think this is a case where the function/Method can be part of the class if I am not mistaken.

All dates need to be looked at once as I am looking for variances in the dates to help detemine which format the data is in.

In the main code I had started with: $material->check_date(); but got the resulting error.
Fatal error: Call to a member function check_date() on a non-object in D:\Documents and Settings\414004425\My Documents\My Websites\PETdb\md4c11.php on line 144

Code: Select all

class Material  {
        public $level;          //Part level
        public $part;           //Part number
        public $part_desc;      //Part description
        public $status;         //Status of part
        public $edate;          //Date the parts are expected
        public $rdate;          //Date the parts are required by
        public $qty;            //Required quantity for project
        public $pr;             //Purchase requistion number to buy part 
        public $po;             //PO numbere that is buying the part
        public $line;           //Line on PO that is purchasing the part
        public $factory;        //Manufacturing facility for part
        public $error;          //SAP error code
        public $prod;           //Production order number
 
        function check_date()  {
            $d0=0;
            $d1=0;
            $d2=0;
            for ($cnt=0; $count <= sizeof($this); ++$cnt) {
                if (!is_null($this[$cnt]->rdate))  {
                    $date_a = explode("/",$this[$cnt]->rdate);
                    if ($d0 > $date_a[0])  {
                        $d0 = $date_a[0];
                        }
                    if ($d1 > $date_a[1])  {
                        $d1 = $date_a[1];
                        }
                    if ($d2 > $date_a[2])  {
                        $d2 = $date_a[2];
                        }
                    }
                }
            if ($d2 > 12) {
                for ($cnt=0; $count <= sizeof($this[$cnt]->rdate); ++$cnt) {
                    if (!is_null($this[$cnt]->rdate))  {
                        $date_a = explode("/",$this[$cnt]->rdate);
                        $this[$cnt]->rdate = $date_a[1]."/".$date_a[2]."/".$date_a[0];
                        $date_a = explode("/",$this[$cnt]->edate);
                        $this[$cnt]->edate = $date_a[1]."/".$date_a[2]."/".$date_a[0];
                        }
                    }
                }
            }
 
        function __construct()  {
            $this->level = NULL;
            $this->part = NULL;
            $this->part_desc = NULL;
            $this->status = "SubcStock";
            $this->edate = NULL;
            $this->rdate = NULL;
            $this->qty = NULL;
            $this->pr = NULL;
            $this->po = NULL;
            $this->line = NULL;
            $this->factory = NULL;
            $this->error = NULL;
            $this->prod = NULL;
            }
        }

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: passing Class to function

Posted: Fri Oct 16, 2009 11:59 am
by JNettles
Do you have 30 separate variables instantiated or is this an array of objects? If $material is really $material[1-30] array, then you can't simply do $material->Functions and have it run on each separate object. Your actual object would be in $material[0]->check_date();

So, to operate on each of them you would need to do a foreach on $material and then execute check_date.

Re: passing Class to function

Posted: Fri Oct 16, 2009 12:02 pm
by pickle
To clean up your code, I'd suggest putting your check_date() function (which you should rename checkDate() to follow conventions) after __construct(). There's no rule saying you have to, but it is a bit of a convention.

I'd also suggest setting your default values right when you declare the object variables, rather than doing it in __construct(). It'll save some lines of code too.

Now, as for the problem you actually asked about:
The reason you're getting that error is because you haven't made an object called "$material". You'll need to instantiate an object first - like this:

Code: Select all

$Material = new Material();
$Material->rdate = "2009-10-10";
$Material->check_date();
It also appears that you don't fully understand what $this is. $this refers to the current object you're in, so $this[$cnt] shouldn't work as that's array syntax. What's the goal of your for() loop? Also, why are you exploding on "/" when the separator will be "-"?

Re: passing Class to function

Posted: Fri Oct 16, 2009 12:35 pm
by dgkindy
To the first response, I have an array of objects Material[1-30]

To the second response:

1. Yes you are correct, I don't totally understanded classes and I am trying to.
2. I have Material[1-30] each of which contain a date.
3. The structure of the data is separated by "/" not the "-" mm/dd/yy or yy/mm/dd are the two formats that I can receive
4. Thanks for the convention tip. Is there somewhere that I can find a list of conventions?
5. To declare the default values would I do

Code: Select all

public $rdate=NULL;
6. Right I don't have an object called "$material".
7. So there is no way to pass all the data to the function/method for the array of Material?

Re: passing Class to function

Posted: Fri Oct 16, 2009 1:58 pm
by JNettles
You've almost got it right in your code, at least in terms of how your method is defined. When defining your object, think solely in terms of individual instances - that is, program it with only one object in mind. Your class shouldn't be trying to operate on every instance of itself. You would use other external functions and procedures for operating on an your array of objects. Each time you declare new Material; you are creating a fully functional and entirely self-contained object in the memory, completely independent of any other instances that you might have.

As for your property declarations, you don't need to declare them as NULL in the __construct, you can do that in the definition stage of your class, like you correctly surmised.

public $count = NULL;

Clean up your method a little bit to reflect what pickle said about using $this (and maybe read up on how its supposed to work) and then give what I said a shot about using foreach on your array of objects.

Code: Select all

 
$material[0] = new Material;
$material[1] = new Material;
$material[2] = new Material;
 
foreach($material as $single_material)
{
       $single_material->check_date();
}

Re: passing Class to function

Posted: Fri Oct 16, 2009 3:18 pm
by dgkindy
Thanks for the help however I am still struggling with the same basic problem.

So I will take another stab at it.

I have a class called material with the follow example data in memory.

$material[0]->date= 09/10/03
$material[1]->date= 09/10/12
$material[2]->date= 09/11/23
$material[3]->date= 09/12/03
$material[4]->date= 09/11/14
$material[5]->date= 09/10/23

The problem I have is sometimes the dates are yy/mm/dd or mm/dd/yy so I want to scan through all dates to try and determine the date structure. The date structure for each import will be the same but need to detemine which format is being used in order to determine whether or not I need to change the formatting or not.

So back to the problem I need to scan through all dates and determine the format and if it is yy/mm/dd then I will convert it to mm/dd/yy

Main code
Call function checkDate(pass array of Material to function)
end

Function checkDate (receive array of Material)

Foreach (value in array of material)
if $day < $Material[]->date
$day = assume date is in mm/dd/yy format
If $day >12 then
do nothing
else
Foreach (value in array of material)
change date from yy/mm/dd to mm/dd/yy
end

So how do I pass the array of material and minupulate it from the function?

Does it work to do

function checkDate(&$Material)
but then that would only be one material and not the array?