Jump to content

Construct Breaks Project


NotSureILikePHP

Recommended Posts

I have a class called Customer and I created a function called Customer with a parameter of id in order to load a customer based on whether they exist already using an id. When I add the function, it breaks the page. I turned error logging on but it's not helping. I get scores of errors mostly undefined index and this method is outdated but no fatal errors and no explanation of why adding this class breaks my page. Here is my code:

 

 

class Customer extends VerySimpleModel
implements TemplateVariable {

    static $meta = array(
        'table' => CUST_TABLE,
        'pk' => array('cust_id'),
        'joins' => array(
            'location' => array(
                'constraint' => array('cust_id' => 'loc.cust_id'),
            ),
            'service' => array(
                'constraint' => array('service_level' => 'service.id'),
            ),
        ),
    );
    
       var $id;
    var $name;
    var $contact;
    var $fname;
    var $lname;
    var $contact_phone;
    var $phone_number;
    var $fax_number;
    var $location;
    var $domain;
    var $service_level;
    var $row;
    var $address;
    var $_location = null;
        
    public function Customer( $id=0 )
    {
        echo 'here';
        $this->id=0;
        if($id && ($info=$this->getInfoById($id)) ){
            $this->row=$info;
            $this->id=$info["id"];
            $this->name=$info["cust_name"];
            $this->domain=$info["domain"];
            $this->location=$this->getLocationInfo($id);
            $this->service_level=$info["service_level"];
        }
    }
  }
 
//page that is loading the Grid data

$custs = Customer::objects();

 

I think it has something to do with the way that $custs is being loaded but I can't find any good detail on what objects() even is in php so I can't really figure out exactly what that line is doing and if that's even the problem.

Link to comment
Share on other sites

What does "breaks the page" mean? And where did you turn error logging on? If it is not turned on in the php.ini itself, then certain types of fatal errors will occur before the error reporting in the code is executed - thus you won't get any errors. Make sure that error_reporting and display_errors are turned on in the php.ini, and then restart apache.

Link to comment
Share on other sites

You are using PHP 4 syntax that is obsolete. You should not use the var keyword, nor should you give a method the same name as the class. In PHP 4 this was how you defined the constructor.  I suggest you read up on how to use classes in PHP 5.

 

Refactored

<?php 
class Customer extends VerySimpleModel implements TemplateVariable {

    public static $meta = array(
        'table' => CUST_TABLE,
        'pk' => array(
            'cust_id'
        ),
        'joins' => array(
            'location' => array(
                'constraint' => array(
                    'cust_id' => 'loc.cust_id'
                ),
            ),
            'service' => array(
                'constraint' => array(
                    'service_level' => 'service.id'
                ),
            ),
        ),
    );
    
    public $id;
    public $name;
    public $contact;
    public $fname;
    public $lname;
    public $contact_phone;
    public $phone_number;
    public $fax_number;
    public $location;
    public $domain;
    public $service_level;
    public $row;
    public $address;
    private $_location = null;
        
    public function getCustomer( $id = 0 )
    {
        echo 'here';
        $this->id=0;
        if($id && ($info=$this->getInfoById($id)) ){
            $this->row=$info;
            $this->id=$info["id"];
            $this->name=$info["cust_name"];
            $this->domain=$info["domain"];
            $this->location=$this->getLocationInfo($id);
            $this->service_level=$info["service_level"];
        }
    }
  }
 
//page that is loading the Grid data

$custs = Customer::objects();
Link to comment
Share on other sites

The error reporting is not turned on in the php.ini file. It was just locally in the page and by breaks the page it was solid white. Though now, it just displays no record in the grid. I just got back in so I'll try turning on errors in the ini file and that should help. Also I will change all his var code to public and read up on the new php.

Link to comment
Share on other sites

Just to make sure I am doing this accurately I changed the code to this

 

 

class Customer extends VerySimpleModel
implements TemplateVariable {

    static $meta = array(
        'table' => CUST_TABLE,
        'pk' => array('cust_id'),
        'joins' => array(
            'location' => array(
                'constraint' => array('cust_id' => 'loc.cust_id'),
            ),
            'service' => array(
                'constraint' => array('service_level' => 'service.id'),
            ),
        ),
    );
    
       public $id;
    public $name;
    public $contact;
    public $fname;
    public $lname;
    public $contact_phone;
    public $phone_number;
    public $fax_number;
    public $location;
    public $domain;
    public $service_level;
    public $row;
    public $address;
    public $_location = null;
        
    public function _construct( $id=0 )
    {
        echo 'here';
        $this->id=0;
        if($id && ($info=$this->getInfoById($id)) ){
            $this->row=$info;
            $this->id=$info["id"];
            $this->name=$info["cust_name"];
            $this->domain=$info["domain"];
            $this->location=$this->getLocationInfo($id);
            $this->service_level=$info["service_level"];
        }
    }
}
 
\\and I call it like this from the other page
$cust = new Customer($_REQUEST['id']);

 

If I do that it should go to the construct function correct? Because I can't get it to print 'here'.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.