calling construct

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dgkindy
Forum Newbie
Posts: 15
Joined: Tue Sep 22, 2009 8:53 pm

calling construct

Post by dgkindy »

I am trying to call my construct to initialize the varialbes. It does not seem to be calling the method. This is not all the code just a snippet.

Code: Select all

     <?php
          $bin = 0;
          $material[$bin] = new Material;
          $material[$bin]->__construct();
 
class Material  {
        public $level;
        public $part;
        public $part_desc;
        public $status;
        public $edate;
        public $rdate;
        public $qty;
        public $pr;
        public $po;
        public $line;
        public $factory;
        public $error;
        public $prod;
        
        function __construct()  {
            $level = NULL;
            $part = NULL;
            $part_desc = NULL;
            $status = SubcStock;
            $edate = NULL;
            $rdate = NULL;
            $qty = NULL;
            $pr = NULL;
            $po = NULL;
            $line = NULL;
            $factory = NULL;
            $error = NULL;
            $prod = NULL;
            }
        }
?>
 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: calling construct

Post by John Cartwright »

Firstly, you do not ever call the constructor directly (except when a child overrides it's parent constructor), it is automatically called when the object is initialized.

Secondly, you need to reference your class properties using $this, i.e.

$level should be $this->level
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: calling construct

Post by markusn00b »

Your class seems pretty bloated there with those member variables. Maybe you should consider reevaluating your application design. Though, I could be completely wrong. ;)

Mark.
dgkindy
Forum Newbie
Posts: 15
Joined: Tue Sep 22, 2009 8:53 pm

Re: calling construct

Post by dgkindy »

John

If I undersetand correctly this is what you are telling me

Code: Select all

 
<?php
                $material[$bin] = new Material;
  //Deleted line     $material[$bin]->__construct();
    
 
 
class Material  {
        public $level;
        public $part;
        public $part_desc;
        public $status;
        public $edate;
        public $rdate;
        public $qty;
        public $pr;
        public $po;
        public $line;
        public $factory;
        public $error;
        public $prod;
        
        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;
            }
        }
?>
dgkindy
Forum Newbie
Posts: 15
Joined: Tue Sep 22, 2009 8:53 pm

Re: calling construct

Post by dgkindy »

markusn00b
Your class seems pretty bloated there with those member variables. Maybe you should consider reevaluating your application design. Though, I could be completely wrong.
Are you suggesting that there are too many variables? The code that I am trying to create is designed to import a text file. All the variables are various data points that I am trying to capture.

I have included a copy of the entire code to date if you care to have a look.

Appreciate teh help and suggestions.

Code: Select all

<?php
 
// System variables
$skip_first_blank_lines=6;
$file_name = "Text.txt";
//$file_name = "Hamma.txt";
//$file_name = "text2.txt";
 
// Open file in read mode
$file = @fopen($file_name, "r") or exit("Unable to open file!");
if ($file) {
    $sLBL=fgets($file);
    $project = project($sLBL);  //Isolates project number from first row of text in file 
    echo "Project:  ".$project."<br /><br />";
    //Skips through blank lines in file
    for ($count=0; $count <=$skip_first_blank_lines; ++$count) {
        $sLBL=fgets($file);
    }
 
    $new_part_flag = 1; //Indicates whether a new part should be created
    $part_cnt = 0;     //Counts the number of parts in file
    $cnt = 0;
    $bin = 0;
    $level = 1;
    //Loops through the material status lines until end of file
    while (!feof($file)) {
        $pos=0;
        if (trim($sLBL,"| -") != "") {  //Skips blank lines in text file
            while (substr($sLBL,$pos,1) == '|') {   //Determine part level
                $pos++;
            }
            if ($level < $pos)  {  //Determines if new part
                $bin = $part_cnt++;
                $bookmark[$cnt++] = $bin;
                $material[$bin] = new Material;
                }
            elseif($level > $pos)  {  //References storage location for incomplete part
                $bin = $bookmark[cnt];
                }
            //Collect part number, name and factory
            If (substr($sLBL,$pos,2) == '--') {
                $material[$bin]->level = $pos;
                $pos=$pos+2;
                $material[$bin]->part  = component($sLBL,$pos);
                $pos = $pos + strlen($material[$bin]->part);
                while (substr($sLBL,$pos,1) == ' ') {
                    $pos++;
                }
                $material[$bin]->factory = substr($sLBL,($pos - strlen($sLBL)),4);
                $pos=$pos+7;
                $material[$bin]->part_desc = substr($sLBL,($pos - strlen($sLBL)),40);
            }
            //Collect all remaining details regarding a part
            else  {
                $sfragment = explode(" ",ltrim(substr($sLBL,($pos - strlen($sLBL)))));
                if (rank($sfragment[1]) < rank($material[$bin]->status))  {
                    $material[$bin]->status = $sfragment[1];
                }
                
                switch (rank($sfragment[1])) {
                    case 1:  //Plan.order
                        $material[$bin]->edate = fdate($sfragment[0]);
                        break;
                    case 2:  //PurRequist
                        $material[$bin]->edate = fdate($sfragment[0]);
                        list($material[$bin]->po, $material[$bin]->line) = purchase_order($sfragment[2]);
                        break;
                    case 3:  //Sim.reqmts
                        //No action to take
                        break;
                    case 4:  //POSched.ln
                        $material[$bin]->error = error_code($sLBL);    
                        $material[$bin]->edate = fdate($sfragment[0]);
                        list($material[$bin]->po, $material[$bin]->line) = purchase_order($sfragment[2]);
                        break;
                    case 5:  //Ship.note
                        $material[$bin]->edate = fdate($sfragment[0]);
                        list($material[$bin]->po, $material[$bin]->line) = purchase_order($sfragment[2]);
                        break;
                    case 6:  //Sched.agr.
                        $material[$bin]->edate = fdate($sfragment[0]);
                        list($material[$bin]->po, $material[$bin]->line) = purchase_order($sfragment[2]);
                        break;
                    case 7:  //QM InspLot
                        //No action to take
                        break;
                    case 8:  //Prod.order
                        $material[$bin]->error = error_code($sLBL);
                        $material[$bin]->prod= production_order($sfragment[0]);    
                        $material[$bin]->edate = fdate($sfragment[0]);
                        break;
                    case 9:  //Plant Stck
                        break;
                    case 10:  //Depend.req
                        $material[$bin]->qty = quantity($sLBL);    
                        $material[$bin]->edate = fdate($sfragment[0]);
                        break;
                    case 11:  //Order.res.
                        $material[$bin]->qty = quantity($sLBL);    
                        $material[$bin]->rdate = fdate($sfragment[0]);
                        break;
                    case 12:  //SubctctReq
                        $material[$bin]->qty = quantity($sLBL);    
                        $material[$bin]->rdate = fdate($sfragment[0]);
                        break;
                    case 13:  //SalesOrder
                        $material[$bin]->qty = quantity($sLBL);    
                        $material[$bin]->rdate = fdate($sfragment[0]);
                        break;
                    case 14:  //ReleaseOrd
                        $material[$bin]->qty = quantity($sLBL);    
                        $material[$bin]->rdate = fdate($sfragment[0]);
                        list($po[$bin], $line) = purchase_order($sfragment[2]);
                        break;
                    case 15:  //PldOrdRelO
                        $material[$bin]->prod = production_order($sfragment[0]);    
                        $material[$bin]->qty = quantity($sLBL);    
                        $material[$bin]->rdate = fdate($sfragment[0]);
                        break;
                    case 16:  //ProjectOrd
                        //confirm variables
                        break;
                    case 17:  //Free deliv
                        //confirm variables
                        break;
                    case 18:  //SubcStock
                        //confirm variables
                        break;
                    default:
                        echo "Status missing from list: '".$sfragment[1]."'<br />";
                } 
            }
            $sLBL=fgets($file);
        }
    }
    fclose($file);
    echo "<pre>"; var_dump($material); echo "</pre>\n";
}
 
class Material  {
        public $level;
        public $part;
        public $part_desc;
        public $status;
        public $edate;
        public $rdate;
        public $qty;
        public $pr;
        public $po;
        public $line;
        public $factory;
        public $error;
        public $prod;
        
        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;
            }
        }
 
function component($string,$pos) {       //Isolates part number from string
    $assembly = (explode(" ",substr($string,($pos - strlen($string)))));
    return $assembly[0];
    }
function project($string) {             //Isolates project number
    return ltrim(stristr(trim($string)," "), "0 ");
    } 
function production_order($string) {    //Isolates produnction order
        $prod_order = explode("/",$string);   
        return $prod_order[0];
    }
function error_code($string) {          //Isolates error code
        return $errord = trim(substr($string,-68,10));   
    }
function quantity($string) {            //Isolates required quantity
        $qty = explode("-",trim(substr($string,-50,10)));   
        return $qty[0];
    }
function purchase_request($string) {    //Isolates purchase request
        return explode("/",$string);   
    }
function purchase_order($string) {      //Isolates purchase order number and line
        return explode("/",$string);   
    }
function fdate($date) {                 //Isolates date
        $date_a = explode("-",$date);
        return $date = $date_a[0]."/".$date_a[1]."/".$date_a[2];
    }
function rank($status) {                //Determines ranking of status
    switch ($status) {
        case "Plan.order":
          $rank=1;
          break;
        case "PurRequist":
          $rank=2;
          break;
        case "Sim.reqmts":
          $rank=3;
          break;
        case "POSched.ln":
          $rank=4;
          break;
        case "Ship.note":
          $rank=5;
          break;
        case "Sched.agr.":
          $rank=6;
          break;
        case "QM InspLot":
          $rank=7;
          break;
        case "Prod.order":
          $rank=8;
          break;
        case "Plant":  //Plant Stck
          $rank=9;
          break;
        case "Depend.req":
          $rank=10;
          break;
        case "Order.res.":
          $rank=11;
          break;
        case "SubctctReq":
          $rank=12;
          break;
        case "SalesOrder":
          $rank=13;
          break;
        case "ReleaseOrd":
          $rank=14;
          break;
        case "PldOrdRelO":
          $rank=15;
          break;
        case "ProjectOrd":
          $rank=16;
          break;
        case "Free deliv":
          $rank=17;
          break;
        case "SubcStock":
          $rank=18;
          break;
        default:
          $rank=25;
    } 
    return $rank;
} 
?>
Post Reply