944,003 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3198
  • PHP RSS
Mar 17th, 2007
0

Object Oriented Design

Expand Post »
Hello,

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.

Thanks,

Chad
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
petzoldt01 is offline Offline
49 posts
since May 2006
Mar 18th, 2007
0

Re: Object Oriented Design

Hi,

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.

eg:

[PHP]
// short
<?php= $variable; ?>

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

[/PHP]

The "common" shorthand syntax such as:

[PHP]<?php

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

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

// etc...

?>[/PHP]

exists in PHP without any extensions or configuration.

Heres the PHP4 OOP docs:
http://www.php.net/oop

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

[PHP]
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;
}

}
[/PHP]

To view the methods of this class you use:

[PHP]get_class_methods('className')[/PHP]

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:

[PHP]<?php

$myObject = new className();

?>[/PHP]

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

The correct syntax for referencing a method is:

[PHP]$myObject->method1();[/PHP]

for referencing a property:

[PHP]$myObject->property;[/PHP]

To get the list of methods:

[PHP]get_class_methods($myObject)[/PHP]

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:
[PHP]
class myClass extends className {

/** Statically set $myproperty */
var $myproperty = 1;

function myMethod() {
return array($this->property, $this->myproperty);
}

}[/PHP]

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.

eg:

[PHP]
// 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) );
[/PHP]

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.

eg:

[PHP]$myObject->property2 = $myObject2;[/PHP]

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

[PHP]$myObject->property2->myMethod();[/PHP]

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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Mar 18th, 2007
0

Re: Object Oriented Design


[php]$myObject->property2 = $myObject2;[/php]

[php]$myObject->property2->myMethod();[/php]

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?

Thanks
Last edited by petzoldt01; Mar 18th, 2007 at 11:44 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
petzoldt01 is offline Offline
49 posts
since May 2006
Mar 19th, 2007
0

Re: Object Oriented Design

Click to Expand / Collapse  Quote originally posted by petzoldt01 ...
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?

Thanks
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

[PHP]$var1 = 0;
$var2 = $var1; // assign by value

$var2 = 1;[/PHP]

now $var1 = 0 and $var2 = 1. If you want both to be equal to 1 you can do:
[PHP]
$var1 = 0;
$var2 =& $var1; // assign by reference

$var2 = 1;[/PHP]

Now both $var1 and $var2 equal 1.

In the example w/ OOP it would be:

[PHP]$myObject->property2 =& $myObject2; [/PHP]

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

[PHP]function fn_name(&$arg) { .. }[/PHP]

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...
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: www2 and www
Next Thread in PHP Forum Timeline: commenting system and pagination issue : commenting with filw system storage





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC