Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/297769-this-not-working/
Share on other sites

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';
Link to comment
https://forums.phpfreaks.com/topic/297769-this-not-working/#findComment-1518715
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/297769-this-not-working/#findComment-1518716
Share on other sites

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 by mac_gyver
Link to comment
https://forums.phpfreaks.com/topic/297769-this-not-working/#findComment-1518724
Share on other sites

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';
}
Link to comment
https://forums.phpfreaks.com/topic/297769-this-not-working/#findComment-1518725
Share on other sites

 

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.

Link to comment
https://forums.phpfreaks.com/topic/297769-this-not-working/#findComment-1518739
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/297769-this-not-working/#findComment-1518837
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/297769-this-not-working/#findComment-1518838
Share on other sites

... 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.

  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/297769-this-not-working/#findComment-1518923
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/297769-this-not-working/#findComment-1518926
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.