Jump to content

[SOLVED] Not able to pass object ???


percent20

Recommended Posts

i have been working on a site and all was working well then one day spent sometime doing some refactoring and boom all of a sudden one thing on the site doesn't work.  That is trying to pass an object.  Well I should say it passes part of the object.  Here is some of the code

 

Object Data Type

class Link
{
// fields
public $id;
public $link;
public $title;
public $desc;
public $category;
}

 

Basically i am creating an object and setting the properties to the above.  I then create an object that I use to do all my data stuffs with the data base and pass it the object above.  Here is the basics of that.

 

$theLink = new Link();

$theLink->id = $_POST["id"];
$theLink->link = $_POST["link"];
$theLink->title = $_POST["title"];
$theLink->desc = $_POST["desc"];
$theLink->category = $_POST["category"];

$linksData = new LinksData();

if($linksData->UpdateLink($theLink)){
}

 

What is most intrigiung is only the $theLink->link seems to get passed when $linkData->UpdateLink($theLink) is called because it actually updates in the database.

 

However, when I cut out that whole part all together and take everything in the UpdateLink function and put it in with the above code all works just fine.  So that leads me to beleive I have a problem passing the object.

 

For general information about my environment.

PHP 5.2.5

Apache 2.0

Windows XP Pro SP2

Link to comment
Share on other sites

There is nothing wrong with your code as is, and it should work fine. A couple notes...

 

-are you sure the values of $_POST are being passed properly from your form?

-passing an object as an argument actually passes it by reference (unlink normal number/string/array data types)...not sure if this applies to you

 

the following code work just as expected:

<?php
  class Link {
    public $id;
    public $link;
    public $title;
    public $desc;
    public $category;
  }
  class LinksData {
    public function UpdateLink ( $linkObj ) {
      print_r($linkObj);
      return true;
    }
  }
  
  $theLink = new Link();

  $theLink->id = 123;
  $theLink->link = 'http://foobar.com';
  $theLink->title = 'My Test Site';
  $theLink->desc = 'Some notes about it';
  $theLink->category = 'samples';
  
  print_r($theLink);

$linksData = new LinksData();
if($linksData->UpdateLink($theLink)){
  print 'success';
}
?>

Link Object
(
    [id] => 123
    [link] => http://foobar.com
    [title] => My Test Site
    [desc] => Some notes about it
    [category] => samples
)
Link Object
(
    [id] => 123
    [link] => http://foobar.com
    [title] => My Test Site
    [desc] => Some notes about it
    [category] => samples
)
success

Link to comment
Share on other sites

i'm an idiot.  it worked I just had a naming mis-match that was messing everything up. grrrrrr.  I hate a wasted 5 hours on misnaming something.

 

Thanks for the help somone else confirming that it works for sure helped me to look elsewhere and I immediatly found the problem.

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.