Using Data transfer objects between script & HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
matrixbegins
Forum Newbie
Posts: 6
Joined: Mon Aug 13, 2007 11:11 am

Using Data transfer objects between script & HTML

Post by matrixbegins »

Hello Everybody!

Can I use data transfer objects like JAVA BEANS of java in PHP. I'll illustrate my problem with my example.

Here are Few Files that I use in my application:

Code: Select all

1. DataHandler.php            // a simple class that connects to database , closes connection, executes, insert/select/update/ delete queries.     
           
2. UserInfo.php              // a simple class that have some Getters/ setters methods for accessing a user's attributes.
 
 
3. ShowUserInfo.php      // A simple file that authenticate & obtain data of a particular user.
 
4.MyHome.php              // A simple view / HTML to show data.
 
Now whenever a user logged in Showuser.php fetchs data and obtain an object of UserInfo Class, which have some attributes like username, phptoUrl, type(paid/free), Age etc.

Now How ShowUserInfo.php must pass this object to MyHome.php so that it can used as a proper object there.

In order to solve this I passed the object as a session variable as I have to use this object on some other pages too. But on reteriving Values e.g. obj->getUsername(); Php shows an error stating non_object method call or some thing like that.

Please Help. Please tell me also that am I following a good practice or not. as I am new to PHP so I can't get into MVC framework yet. I well be very Obeliged if u could suggest a proper practice to use OOP.
______________________________________________
With Warm Regards
Ankur
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Re: Using Data transfer objects between script & HTML

Post by mattcooper »

Apparently you're trying to get the properties of an object that doesn't exist. Can you please post some code here, as filenames are of no use. Instinctively, I think that your use of session vars may be correct but we'll all need to see some code to be able to help you.
matrixbegins
Forum Newbie
Posts: 6
Joined: Mon Aug 13, 2007 11:11 am

Re: Using Data transfer objects between script & HTML

Post by matrixbegins »

Here Is a simple Example That I used for demo.

I used 5 files.

1.ClassDemo.php
2. script.php
3. pageHtml.php
4.Second.php
5. appfunctions.php

ClassDemo.php
Code:

Code: Select all

<?php
 
class ClassDemo{
    
    private $user, $type;
    
    public function getUser(){
        return $this->user;
    }
    
    public function setUser($user){
        $this->user=$user;
    }
    
    
    public function getType(){
        return $this->type;
    }
    
    public function setType($type){
        $this->type=$type;
    }
}
?>

Now scripts.php
Code:

Code: Select all

<?php
session_start();
 
include_once("ClassDemo.php");
include_once("appFunctions.php");
$user=$_POST["user"];
$type=$POST["type"];
$obj= new ClassDemo();
$obj->setUser($user);
$obj->setType($type);
 
$_SESSION["UserObj"]=$obj;
 
redirect_to("Second.php");
 
?>

Now pageHtml.php
Code:

Code: Select all

<HTML>
 
<BODY>
    <form  name="form1" method="post" action="script.php">
        
  <label>Email
  <input name="user" type="text" id="email" />
  </label>
  <p>
    <label>Password
    <input name="type" type="text" id="pwd" />
    </label>
  </p>
  <p>
    <label>
    <input type="submit" name="Submit" value="Login" />
    </label>
  </p>
</form>
</BODY>
</HTML>
 
Second.php
Code:

Code: Select all

<?php
session_start();
include_once("ClassDemo.php");
$user=new ClassDemo();
$user=$_SESSION["userObj"];
 
echo $user->getUser();
echo"<br>";
echo $user->getType();
?>


Now appFunctions.php

Code:

Code: Select all

<?php
function redirect_to( $location = NULL ) {
        if ($location != NULL) {
            header("Location: {$location}");
            exit;
        }
    }
?>
Finally when I run this whole assembly I encountered with this error !!

Fatal error: Call to a member function getUser() on a non-object in C:\wamp\www\demoapp\second.php on line 7.

Now I think this simple to understand.

One more thing
First I write second.php as
Code:

Code: Select all

<?php
session_start();
$user=$_SESSION["userObj"];
 
echo $user->getUser();
echo"<br>";
echo $user->getType();
?>
 
But I got Same error !!

Please Help me out
matrixbegins
Forum Newbie
Posts: 6
Joined: Mon Aug 13, 2007 11:11 am

SOLVED :: Using Data transfer objects between script & HTML

Post by matrixbegins »

Hi ! Every body!! I got Solutions

ERROR 1
scripts.php
Line 7

Code: Select all

$type=$POST["type"];
change it to

Code: Select all

$type=$_POST["type"];
ERROR 2
script.php
Line 12

Code: Select all

$_SESSION["UserObj"]=$obj;
& second.php
Line 4 & 5

Code: Select all

 
 $user=new ClassDemo();
 $user=$_SESSION["userObj"];
 
No need of line no. 4 and Case mismatch in line 5 userObj it should be UserObj
Post Reply