0

I'm having issues accessing the Jobinfo class from my Deliveries. The problem is I need to be able to get the value of getQty from my child class and I also need to be able to get the qty_ship method using a property from the parent. How can I do this? It doesn't seem to work and quite confused over this... I'd like to be able to use methods from Parent->Child and Child->Parent dynamically.

class jobInfo
{
    public $JOB_ID;
    private $deliveries; // DELIVERIES CLASS

    function __construct($job_id)
    {

        $this->JOB_ID=$job_id;
        $this->deliveries = new Deliveries();

    }
    public function getQty()
    {
         return $this->query_s('job_sheet','*', 'job_id',$this->JOB_ID, 1, 'qty');
         //returns a quantity from query method
    }

}


class Deliveries  extends jobInfo
{

    function __construct(){}

    public function qty_ship()
    {
        $qty = 0;
        $SQL = mysql_query("SELECT * FROM deliveries WHERE jID='".parent::JOB_ID."'") or die(mysql_error());

        $rows = mysql_num_rows($SQL);
        if($rows>0)
        {
            while($data = mysql_fetch_array($SQL))
            {
                $qty += $data['qty_shipped'];
            }
        }
        return $qty;
     }

     public function getTotalBO()
     {
         $qty =  parent::getQty();
         $totalship =  $this->qty_ship();

         $x = $qty-$totalship;
         return $x; 
     }
 }

$A = new Jobinfo(15);
Dimitri
  • 1,906
  • 3
  • 25
  • 49
  • Not sure if you've searched, but this has been asked quite a few times. [Take a look at this answer.](http://stackoverflow.com/questions/2423396/parent-object-in-php) – Matthew Blancarte Nov 27 '12 at 04:46
  • Well it does look something like that. Basically what I wanted to do, is be able to use some from the parent class and have the parent use something from the child class. But I can't seem to get it... – Dimitri Nov 27 '12 at 04:52

2 Answers2

1

You want $this->getQty() and $this->JOB_ID, but for completeness, consider:

  1. Removing your empty no-parm constructor, as it cannot actually be used to instantiate the class unless it calls the parent contructor with a job id, which it cannot do since you expect the job id to be defined externally.

  2. Making JOB_ID protected. For better encapsulation you might instead make it private and provide a getJobId() method.

  3. Working on consistent class naming - jobInfo starts with lowercase and Deliveries starts with uppercase.

  4. Working on consistent function naming - You have underscore-seperated functions mixed with camel-case functions.

  5. Working on consistent spacing - You mix 1-character, 2-character and 0-character spacings throughout without much rhyme or reason.

Welcome to OOP and Good Luck!

David Farrell
  • 3,492
  • 2
  • 15
  • 11
  • Thanks for the post. I have an idea of what to do from this point on. It's different working on projects in C# than in PHP... About consistency, you're right. A lot of these methods were derived from old functions on the site and never thought it would ever be used in a class. Thanks for pointing out the naming for the class names also. – Dimitri Nov 27 '12 at 12:26
0

If you extend from that class, then you should be using

$this->getQty();

$this will return the current object wich already include the parent with all the public and protected variables and methods.

You should only use :: when you call a static class

for exemple :

jobInfo::getQty();

You might also want to take a look at the naming convention.

http://framework.zend.com/manual/1.12/en/coding-standard.naming-conventions.html

user1855153
  • 579
  • 4
  • 15