Solved

Object Oriented PHP

Posted on 2004-07-31
2
225 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

624 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