Solved

Object Oriented PHP

Posted on 2004-07-31
2
224 Views
Last Modified: 2009-07-29
I'm trying to convert some Java code into PHP code:

I created a class called Product in PHP, and a class called ProductDB which has utility functions for the database.  The java code is:

Product product = ProductDB.readRecord(connection, productCode);

Are there static methods in PHP?  Would I have to do something like the following?

$product = new Product();
$productdb = new ProductDB();
$product = $productdb->readRecord($connection, $productCode);

Also how do user defined objects work with sessions ( adding/retrieving) ?
0
Comment
Question by:abstractionz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Expert Comment

by:meet_zorrer
ID: 11687051
Hi,

Ans 1) there are no static methods till php version 4.3
Ans 2) if you want some function of your class to return an object of other class just do it like the following code

//Start of some [ test_script.php ]
//~~~~~~~~~~~~~
require_once("product.php"); //this file contains the product related logic
require_once("productDB.php"); //this file has the database logic related to products

//you see the product file needs to be include/required first/before the productDB.php file becos this will help the productDB.php file to return an object variable through it's member function

Now your code's turn

//$product = new Product(); //no need of this your $product variable will be instentiated and will be valid when returned
$productdb = new ProductDB();
$product = $productdb->readRecord($connection, $productCode);

//~~~~~~~~~~~~~~~
//Start of some [ test_script.php ]

now in your member function [readRecord] of productDB class
you will have to create a new object of product class and set it's properties by code
like this before return from the function

$record = $productdb->readRecord($connection, $productCode);
//if $record = array then

$temp_prod = new Product();
$temp_prod->name = $record["name"]
$temp_prod->id = $record["id"];

//and so on to the other properties that you get from $record object

Hope  this helped...
Regards,
Meet
0
 
LVL 2

Accepted Solution

by:
d_tan earned 75 total points
ID: 11690243
I would highly suggest going to PHP5 if you are converting from JAVA.  This is because, in PHP5 the object model has changed significantly to use object references (like JAVA).  Likewise, there are a ton of new features which make the new version look a lot like (and feel like JAVA) as well.  

Furthermore your code:

Product product = ProductDB.readRecord(connection, productCode);

would be identical if you declare readRecord a static function in php 5.  Also, user defined objects can be easily added to the session.  Same as usual . . . $_SESSION['myObject'] = $myObect . . . .same for retriveing.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question