473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Object Oriented PHP

Hello again,

I have written three classes : validate.class.php,
report_handler.class.php, sentry.class.php.

The problem is that the "validate" class uses the "reporting" class and
the "sentry" uses the "validate" that uses the "reporting".

So I end up having something like that inside sentry.class.php:
$validate->report->getReport();

But although that's working... it doesn't seem to be really OO :p

So can you tell me what I can do ?

If what I am saying doesn't make any sense at all... let me know and I
will try to explain it better.

Bellow I have some snippets from each class that may help you to
understand what I am trying to do.
************************************************** ******************
class report_handler {
....
/* This Function populates the array with reports (ERRORS,MESSAGES)
* Syntax: setReport($reportType,$string)
*/
function setReport($reportType,$string='') {
if($this->validateReportTypes($reportType)) {
$this->counter++;
$this->report[$this->counter] = array( 'category' => $reportType,
'description' => $string);
}
else
echo "Report type '".$reportType."' is not valid";
}
/* Output the report to the user
*/
function getReport() {
if(is_array($this->report))
{
foreach($this->report as $i => $value)
{
echo "<pre>".
$value['category'].': '.
$value['description']
."</pre>";
}
}
}
....
}
/*****************END of class report_handler********************/

class validate {

function validate()
{
$this->report = new report_handler();
}

function checkEmail($email)
{
// checks proper syntax
if(
!preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/',
$email))
{
$this->report->setReport('Error','Syntax Error');
return false;
}
....
}
}
/*****************END of class validate ********************/
class sentry {
var $user_id;
var $session_timeout;

function sentry()
{
$this->report = new report_handler;
}
....

function validateLoginForm()
{
$validate = new validate();
if($validate->checkEmail($_POST['email']))
{
$this->loginUserName = $_POST['email'];
$this->loginPassword = $_POST['passwd'];
return true;
}
else
{
$validate->report->getReport();
return false;
}
}
.....
}
/*****************END of class sentry********************/


Thanks a lot for reading and for any help you may provide.
Angelos.
Nov 23 '05 #1
6 1479
>But although that's working... it doesn't seem to be really OO :p

Sure it is. OO is just as much aggregation and composition as it is
inheritance. In fact, inheritance limits you in that you can't change
the inheritance at runtime (well, in PHP you can, but OO purists would
have a heart attack). You can modify the composition, however.

In your case, you could mimic your "uses" idea with inheritance. For
instance, you could have validate extend reporting and have sentry
extend validate. But you also need to consider if this makes sense.
Is sentry really a more specific instance of validate? Is validate
really a more specific instance of report? I think the answer is no.
You probably want to use validate with things other than reports.

Based on your example, I think you could probably make validate() a
static method and pass it a report object as a parameter. Or better
yet, create an interface (or abstract class) called "Validatable" or
something. This interface should define all the methods you might need
to validate something (such as getContent(), getEmail(), or
setError()). The report class can then implement/extend Validatable.
Then, inside your validate() method, check that the parameter is an
instance of Validatable and do what you need to do. This way, you can
make anything validatable (an email address, form input, etc.) and just
pass it in.

Nov 23 '05 #2
i wouldn't use the variable report directly.
$validate->report->getReport();

i'd make:
$validate->getReport();

and in class validate, make a method:
function getReport(){
$this->report->getReport();
}

seems a little more OO to me
(well at least, at OO classes i learnt to make variables private)

Nov 23 '05 #3
From a pure OO perspective, 'validate' is a method not an object.
What you really have is an emailAddress object that requires a validate
method.

Something like:
$email = new emailAddress($_POST['email']);
if( $email->isValid() {
...
} else {
...
}

-david-

Angelos wrote:
Hello again,

I have written three classes : validate.class.php,
report_handler.class.php, sentry.class.php.

The problem is that the "validate" class uses the "reporting" class and
the "sentry" uses the "validate" that uses the "reporting".

So I end up having something like that inside sentry.class.php:
$validate->report->getReport();

But although that's working... it doesn't seem to be really OO :p

So can you tell me what I can do ?

If what I am saying doesn't make any sense at all... let me know and I
will try to explain it better.

Bellow I have some snippets from each class that may help you to
understand what I am trying to do.
************************************************** ******************
class report_handler {
...
/* This Function populates the array with reports (ERRORS,MESSAGES)
* Syntax: setReport($reportType,$string)
*/
function setReport($reportType,$string='') {
if($this->validateReportTypes($reportType)) {
$this->counter++;
$this->report[$this->counter] = array( 'category' =>
$reportType,
'description' =>
$string);
}
else
echo "Report type '".$reportType."' is not valid";
}
/* Output the report to the user
*/
function getReport() {
if(is_array($this->report))
{
foreach($this->report as $i => $value)
{
echo "<pre>".
$value['category'].': '.
$value['description']
."</pre>";
}
}
}
...
}
/*****************END of class report_handler********************/

class validate {

function validate()
{
$this->report = new report_handler();
}

function checkEmail($email)
{
// checks proper syntax
if(
!preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/',
$email))
{
$this->report->setReport('Error','Syntax Error');
return false;
}
....
}
}
/*****************END of class validate ********************/
class sentry {
var $user_id;
var $session_timeout;

function sentry()
{
$this->report = new report_handler;
}
....

function validateLoginForm()
{
$validate = new validate();
if($validate->checkEmail($_POST['email']))
{
$this->loginUserName = $_POST['email'];
$this->loginPassword = $_POST['passwd'];
return true;
}
else
{
$validate->report->getReport();
return false;
}
}
....
}
/*****************END of class sentry********************/


Thanks a lot for reading and for any help you may provide.
Angelos.


Nov 23 '05 #4
Angelos wrote:
But although that's working... it doesn't seem to be really OO :p

So can you tell me what I can do ?


Errrr, stop pretending that you have an OOP design? You're basically
programming procedurally with the use of classes. If your class methods
are regular regular functions, you wouldn't be having the problem
you're having now.

One of the tenets of OOP is encapsulation--codes deal with a different
concerns should be independent and self-contained. When you have one
class that does one thing (say validation) directly dependent on
another class, doing something quite unrelated (say logging), the
design is rubbish. For the situation you described, you would want some
kind of slot/socket mechanism. In a typical PHP script though, such
complexity is really an overkill.

Nov 24 '05 #5
> Errrr, stop pretending that you have an OOP design?

I don't pretend to have an OOP design... I know that it is close but not
OO !
One of the tenets of OOP is encapsulation--codes deal with a different
concerns should be independent and self-contained. When you have one
class that does one thing (say validation) directly dependent on
another class, doing something quite unrelated (say logging), the
design is rubbish.
Why do you say that the design is rubbish ? In OO everything can relate
to anything ... Who says that a logging Class should not relate with a
Validation Class...
For the situation you described, you would want some
kind of slot/socket mechanism. In a typical PHP script though, such
complexity is really an overkill.


I don't even know how you went into socket programming...
/************************************************** *********************/
First of all... I am not going to write a complete OO script because
full OO support in PHP was added in version 5 and my server doesn't
support Ver 5.

Second the only reason that I am using Classes and try to apply OO
concepts in my script is that I want to write reusable code.

I want to have modular program that its module is independent and can be
installed or uninstalled in any time.

I used to program in JAVA and I was really used to Analysis Design and
Implementation process but the last year I started learning PHP and got
lazy...

Anyway thanks for the replies, the "extend" of a class is a solution but
not the best ... I am going to start re-designing something that makes
more sense ...
Nov 24 '05 #6
Angelos wrote:
Why do you say that the design is rubbish ? In OO everything can relate
to anything ... Who says that a logging Class should not relate with a
Validation Class...


The basic premise of OO is that objects in a program behave as objects
in the real world. A class should only be dependent on classes that are
its constituent parts. A log meant to be used across an application
obviously isn't be a part of the data validator.

In what I consider a proper OO design, the validator class would expose
a connection point that is then plugged into a slot in the logging
class. The two classes remain independent of each other. One broadcasts
signals to any possible listeners, while the other handle signals
emitted by any possible sources. If suddenly I don't want logging, all
I have to do is break the connection. If I want to add another
notification mechanism, I just grab another connection point and stick
it somewhere else. Neither case involves a change to the class.

Of course, a full-on OO treatment is overkill for most PHP apps. Going
through multiple layers of abstractions to echo a line of text is vain.
A half-hearted attempt at OO, I would say though, is pointless and
counterproductive. You have functions defined somewhere that you want
to call--that's procedure oriented programming. Wrapping them in a
class doesn't change anything--the basic approach is still the
same--except now you have a object to worry about.

Nov 24 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Benjamin C. Pierce | last post by:
The Twelth International Workshop on Foundations of Object-Oriented Languges (FOOL 12) Saturday 15 January 2005 Long Beach, California, USA Following POPL 05 The search for sound principles...
5
by: Martin | last post by:
When was inheritance intruduced into object oriented programming? More generally, does anyone know or have any sources on when the different features were introduced into object oriented...
34
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. ...
65
by: Roger Smythe | last post by:
A means for the progressive decomposition a problem space into increasingly simpler component parts such that these component parts represent higher levels of conceptual abstraction, and are...
14
by: Rookie | last post by:
Is C an object oriented programming language?
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
8
by: cppig1995 | last post by:
C++ is not only object-oriented language. I think the description of this group have to be changed. :-) Have fun! ---- cppig1995 (cppig1995@gmail.com) Daejeon Metropolitan City, Republic of...
47
by: Thierry Chappuis | last post by:
Hi, I'm interested in techniques used to program in an object-oriented way using the C ANSI language. I'm studying the GObject library and Laurent Deniau's OOPC framework published on his web...
46
by: ajba74 | last post by:
Hi fellows, I am reading some books to learn the C programming language, and sometimes I have the feeling that when somebody becomes a C expert, he must learn a more modern and object-oriented...
3
by: notnorwegian | last post by:
i have some confusion over this. sure a class is basically a classification, like for example an animal or flower. and an object/instance of that class is then for example a cat. an object is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.