I am attempting to create a session class. I want it to be able to store all kinds of data about the current session, such as member information, authentication, application data, or any other structures that would be unique to any given user.

If I were to create a new class called Session. Is there a simple way of maintaining all of the child classes that are required for that particular Session instance? I am making this class very flexible, so there could be any number of child classes. The Session class will be a data container for the most part, but will handle several operations that make the storing and getting of information across pages easier. It will need to accomodate any classes that request its use.

I understand the concept of Object Oriented Programming very well, but I do not know the PHP syntax very well. I know the basic syntax, but I do not know any shortcuts in scripting, such as the numerous shortcuts in PERL, or whether shortcuts even exist. I know how I want the object to work, but I am not sure how deeply object oriented PHP is, and how it differs from other languages' implementation of OOP.

If anyone could just give me a little insight on how to set up classes as "properties" of another class, I would really appreciate it, or if you know of any good references.




To get to know the PHP syntax and functions faster I'd recommend downloading the manual: http://www.php.net/download-docs.php

Some of the things you'll have to look at first in PHP manual is how it handles datatypes. PHP types are not explicitly defined. For example, you can have a condition that matches a string against an integer. The string will be treated as the integer value. eg: if ('1' == 1) is TRUE. To compare types, you need to use ===.

Heres a google search for "PHP OOP" that may help with OOP: http://www.google.com.fj/search?q=PHP+OOP

PHP4 and PHP5 have unique differences in approaching OOP. You'll have to choose which you are targeting or stick to what works for both.

PHP has shorthand syntax support, but it has to be enabled in the configuration, which is found in the file PHP.ini. (you can google that) so I recommend sticking to regular PHP syntax.


// short
<?php= $variable; ?>

// long
<?php echo $variable; ?>

The "common" shorthand syntax such as:


// short
$variable = (condition) ? 'value if true' : 'value if false';

// long
if (condition) {
$variable = 'value if true';
} else {
$variable = 'value if false';

// etc...


exists in PHP without any extensions or configuration.

Heres the PHP4 OOP docs:

I assume you know the basic syntax for creating a class (Object) in PHP.
Here's a simple class as an example:

class className {

	/** statically define a property of className */
	var $property;
	* constructor - same name as Class
	* This will be called whenever a new Object is created with the "new className()" syntax
	function className() {
		// $this refers the this class Instance - when called from an Object created with "new className()".
		$this->property = 1;
	/** method of className */
	function method1() {
	   $this->property = 2;
	/** method of className */
	function method2() {
	   return $this->property;


To view the methods of this class you use:


This returns an array of methods of the class. You can dump the array using var_dump().

To create an Object from the class you use:


$myObject = new className();


The object $myObject will have the methods: className(), method1(), method2() and the property "property".

The correct syntax for referencing a method is:


for referencing a property:


To get the list of methods:


Note that you are passing an object to get_class_methods(). This returns the methods of the object. If you pass a string such as "className" as done previously, it returns the methods of the class defined statically.

To create a class that extends the class "className" use the "extends" keyword:

class myClass extends className {

	/** Statically set $myproperty */
	var $myproperty = 1;
	function myMethod() {
		return array($this->property, $this->myproperty);


Now if you take a look at the properties and methods of an Object Instance of myClass, it will have inherited those of className also.


// create Object - Instance of myClass
$myObject2 = new myClass();

// notice that it has the methods/properties of className also
var_dump( get_class_methods($myObject2) );
echo '<br />';
var_dump( get_object_vars($myObject2) );

One thing to note is that inheritance goes only one way. myClass inherits the methods and properties of className but className does not inherit anything. So php makes you have to kind of program backwards since the parent, that the child inherits from, knows nothing of the child.

Another way of inheriting an Objects methods/properties is to make it a property of another Object dynamically.


$myObject->property2 = $myObject2;

Then you can call the methods of $myObject2 like such:


I like using this personally, as an Object can inherit from many other Objects without having to statically write "extends" into every class you want to inherit.

Note: This is based on PHP4.

$myObject->property2 = $myObject2;

Note: This is based on PHP4.

Thankyou! Your reply was very helpful, and the code above is exactly what I was looking for.

Are there any other ways of doing the same thing, or any shortcuts? I also noticed that you said the code was based on PHP4. Is the code above still valid for PHP5?


Thankyou! Your reply was very helpful, and the code above is exactly what I was looking for.

Are there any other ways of doing the same thing, or any shortcuts? I also noticed that you said the code was based on PHP4. Is the code above still valid for PHP5?


What do you mean by shortcuts? Could you give an example .. maybe even with PERL?

Yes, its valid for PHP5.

Another thing that may be useful.

eg: with PHP4

$var1 = 0;
$var2 = $var1; // assign by value

$var2 = 1;

now $var1 = 0 and $var2 = 1. If you want both to be equal to 1 you can do:

$var1 = 0;
$var2 =& $var1; // assign by reference

$var2 = 1;

Now both $var1 and $var2 equal 1.

In the example w/ OOP it would be:

$myObject->property2 =& $myObject2;

When writing functions you can also make arguments pass by reference using:

function fn_name(&$arg) { .. }

This works both in PHP4 and PHP5. (In PHP5 passing by reference during a function call is not allowed however. eg: fn_name(&$somevar); // gives an error)

PHP5 has "better" support for OOP so I hear. It does have a useful built in class called "reflection" that allows you to inspect the methods/properties and I should believe create your own functions that ease inheritance...