NotSureILikePHP Posted August 13, 2015 Share Posted August 13, 2015 I am having trouble with $this in a class file. Here's my code: function getId() { return $this->cust_id; } function update($vars, &$errors, $location_number=0, $mass=true){ return $this->save($this->getId(), $vars, $errors, $location_number, $mass )?true:false; }function save( $id, $vars, &$errors, $location_number, $mass){ echo 'now'; } [/] I know there is no customer id, as this is a new client but this is how the code was before and it was working. However, even if I pass a 0 for the customer id and do this: [code] function update($vars, &$errors, $location_number=0, $mass=true){ return $this->save(0, $vars, $errors, $location_number, $mass )?true:false; } [./] It still fails to call the save function. I have to do the following: [code] function update($vars, &$errors, $location_number=0, $mass=true){ return Cust::save(0, $vars, $errors, $location_number, $mass )?true:false; } [/] even though I am already in the Cust class. What is wrong? Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 13, 2015 Share Posted August 13, 2015 How are you calling update()? Quote Link to comment Share on other sites More sharing options...
NotSureILikePHP Posted August 13, 2015 Author Share Posted August 13, 2015 Like this: if(!isset($_POST['add'])){ if($thisstaff->isAdmin()) if( Cust::update($_POST,$errors,0)){ $msg='Customer record created successfully'; $_POST=null; }elseif(!$errors['err']) $errors['err']='Error creating customer record'; Quote Link to comment Share on other sites More sharing options...
NotSureILikePHP Posted August 13, 2015 Author Share Posted August 13, 2015 I meant to format that better before I sent but my keyboard seems to have a mind of it's own at the moment... if($thisstaff->isAdmin()) if( Cust::update($_POST,$errors,0)){ $msg='Customer record created successfully'; $_POST=null; }elseif(!$errors['err']) $errors['err']='Error creating customer record'; However, I can echo out statements in the update function. I can even make it to the save function so I wouldn't think the initial call would be the problem. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 13, 2015 Share Posted August 13, 2015 (edited) what you are using for the forum's [/nobbc] bbcode tags isn't working, as you can see since your posted code isn't contained within a color-highlighted box -<?php// your php code here...echo "hello world";?> i suspect that your closing bbcode tags isn't [nobbc][/code] ? Edited August 13, 2015 by mac_gyver Quote Link to comment Share on other sites More sharing options...
NotSureILikePHP Posted August 13, 2015 Author Share Posted August 13, 2015 No it was [ / ] without the spaces. That's what I was told sorry maybe this will work better. if(!isset($_POST['add'])){ $customer = new Customer(); if($customer && $thisuser->canEditCustomer()) if( $customer->update($_POST,$errors)){ $msg='Customer record created successfully'; $_POST=null; }elseif(!$errors['err']) $errors['err']='Error creating customer record'; } Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 13, 2015 Share Posted August 13, 2015 if( Cust::update($_POST,$errors,0)){ That's what I figured. Since you didn't create an instance of Cust, you cannot use $this. $this references the instance of the class in which it is called. Also, you shouldn't be calling methods statically unless they are explicitly labeled as static. Quote Link to comment Share on other sites More sharing options...
akphidelt2007 Posted August 14, 2015 Share Posted August 14, 2015 (edited) Edit:: nevermind... scootstah is right. Edited August 14, 2015 by akphidelt2007 Quote Link to comment Share on other sites More sharing options...
NotSureILikePHP Posted August 14, 2015 Author Share Posted August 14, 2015 Okay so I changed my code to the following page 1: if(!isset($_POST['add'])){ $customer = new Cust(); if($customer && $thisstaff->isAdmin()) { if( $customer->update($_POST,$errors,0)){ $msg='Customer record created successfully'; $_POST=null; }elseif(!$errors['err']) $errors['err']='Error creating customer record'; } } page 2 function update($vars, &$errors, $myID=null, $location_number=0, $mass=true){ return $this->save($this->getId(), $vars, $errors, $location_number, $mass )?true:false; } function getId() { return $this->cust_id; } And it breaks. I get a blank page. However, if I do the following it works... function update($vars, &$errors, $myID=null, $location_number=0, $mass=true){ return $this->save($myID, $vars, $errors, $location_number, $mass )?true:false; } This is why I changed it to begin with. For some reason it doesn't like that "$this->getId()" function. Thanks for the static/non static tip. Quote Link to comment Share on other sites More sharing options...
NotSureILikePHP Posted August 14, 2015 Author Share Posted August 14, 2015 I think I figured it out. Because it's null it seems to be throwing an error though it didn't do that in the old code. Not sure why the change but when I did this it didn't throw an error. if (isset($this->cust_id)) { return $this->cust_id; } return 0; Php seems to be really weird about tags. For instance the old coder did a lot of "<? ?>" tags and if I change one to "<?php ?>" it freaks out and breaks the whole page. And, the reason I have to change some is because the page isn't working properly even though it did in the old website. Quote Link to comment Share on other sites More sharing options...
maxxd Posted August 15, 2015 Share Posted August 15, 2015 ... I get a blank page. ... Turn on error reporting. Add error_reporting(-1); ini_set('display_errors',true); to the top of your script. As for the <? ?> versus <?php ?> tags, there's a couple things that could be going on there. Short tags (<?) used to be OK to use, but they're really frowned upon nowadays, and many servers have turned off the ability to parse them as PHP script tags. However, '<?=' is still a completely acceptable opening tag when you want to simply print a PHP variable or function output. For example, <? echo $myVariable; ?> most likely will not work on the majority of modern servers. <?php echo $myVariable; ?> absolutely will work on all servers. Also, <p>This is HTML, and PHP says <?= $myVariable; ?> because I told it to!</p> will work on all servers, and print the value of $myVariable into the HTML, much like using <p>This is HTML, and PHP says <?php echo $myVariable; ?> because I told it to!</p> Long story short, if you're changing '<?' to '<?php', you shouldn't have any trouble. However, if you're changing '<?=' to '<?php', output will start to disappear. 1 Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 15, 2015 Share Posted August 15, 2015 Long story short, if you're changing '<?' to '<?php', you shouldn't have any trouble. However, if you're changing '<?=' to '<?php', output will start to disappear. And then, never use <? or <?= again. That is by far the stupidest decision the PHP team has ever made. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.