Solved

PHP5 - OOP Question

Posted on 2006-07-05
8
446 Views
Last Modified: 2008-03-10
Hi all,

Say I have 3 classes: Class A, Class B and Class C:

class A {
 var $properties;
 function A () {
  $this->properties = array();
 }
 .....
 .....
 function find ($columns, $cond) {
  $datas = FetchData($this->table, $columns, $cond);
  foreach ($datas as $data) {
    $obj = new AnotherClass();
    $obj = $obj->myObject($data);
    $NOTE_THIS_VARIABLE = $obj->properties['pkValue'];
    if (!empty($obj->properties['class'])) {
        $obj = Factory::createObject('B');        // NOTE: PROBLEM STATE 1 ; Go to Factory class.
     }
     $result[$obj->key] = $obj;
   }
   return $result;
 }
 ...
 ...
}

class Factory {
  public static function createObject($class = 'B') {
     $obj = new $class(); // NOTE: PROBLEM STATE 2
     return $obj;
  }
}


class B extends C {
 function myFunction() {
  $this->variables['field'] = array(1,2,3);
  ....
  ....
  if (!empty($this->properties['pkValue'])) {         // NOTE: PROBLEM STATE 3
    $this->fields['i_want_this'] = array(1,5,7);  // I want this line to be executed !!!!!!!!!
  }
 }
}


class C extends A {  // Note: Class C Extends Class A !!
  function C () {
   $this->variables = array();
   $this->myFunction();  // Calls myFunction() function of class B.
  }
}

The original code lines are more and quite complex to post here, but this is the way it works. I commented the problem lines as "NOTE: PROBLEM STATE ..".

The mainpage calls class A. When it reaches the "PROBLEM STATE 1", it calls the Factory class. The factory class, calls Class B. So, the constructors of Class A and Class C are first executed (coz' Class C extends A and class B extends Class C). When it comes to the "PROBLEM STATE 3", I want the IF loop to be true and execute the line inside of it. I want the $this->properties['pkValue'] value as the value of $NOTE_THIS_VARIABLE.

Please note there is a line: $obj = new $class(); in the Factory class. So the object is NEW and cannot hold value which was defined previously!

Kindly let me know if you want more details. I think there should be an easy way, but I cant trace it. Thank you all.
0
Comment
Question by:karthikramanathan
8 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 17046525
I think a problem may lie in your assumption:
>So, the constructors of Class A and Class C are first executed

There will be only called one constructor. If you have defined one for class B, then that one will be called. If not defined, the constructor of Class C will be called. And if that one not exists, Class A constructor will be called.

For A constructor to be called in C constructor, you would have to use: parent::A(); inside C constructor.

When you have defined your constructors as __construct() - as described in http://php.net/manual/en/language.oop5.decon.php - then you would use parent::__construct();

I hope this helps.

-r-
0
 

Author Comment

by:karthikramanathan
ID: 17048924
>> There will be only called one constructor. If you have defined one for class B, then that one will be called. If not defined, the constructor of Class C will be called. And if that one not exists, Class A constructor will be called.
You are right, Thank you for the guidance.

I can call the Class A constructor in Class C, but I want the value of $this->properties['pkValue'] same as $NOTE_THIS_VARIABLE. Is that possible? Thanks again.

0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17048938
Please elaborate on the "same as".

I don't understand what is not working. You can just set properties['pkValue'] somewhere, and read it elsewhere.

-r-
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:karthikramanathan
ID: 17049043
Sure I will. The value of $this->properties['pkValue'] must be same as the value of $NOTE_THIS_VARIABLE.

Actually, this line in Class A:

$obj = Factory::createObject('B');

should be changed to:

$obj = Factory::createObject($obj->properties['class']);

The foreach loop in Class A is responsible for creating value for $NOTE_THIS_VARIABLE. There are more other classes like Class B. Lets say, they are Class X, Class Y, etc.. So the foreach loop calls different classes each time. I am only concerned Class B because its the only Class which has this IF condition:

if (!empty($this->properties['pkValue']))

>> I don't understand what is not working. You can just set properties['pkValue'] somewhere, and read it elsewhere.
Thats what the problem is! I dont know where to set the value for properties['pkValue'] !! I tried in class A:

$NOTE_THIS_VARIABLE = $obj->properties['pkValue'];
if (!empty($obj->properties['class'])) {
        $this->properties['pkValue'] = $NOTE_THIS_VARIABLE;
        $obj = Factory::createObject('B');        // NOTE: PROBLEM STATE 1 ; Go to Factory class.
}

but it doesn't work. I think the reason is that a new object instance is created in Factory class. Line:

$obj = new $class();

So, the line "$this->properties['pkValue'] = $NOTE_THIS_VARIABLE;" doesn't make sense anymore!
0
 
LVL 14

Expert Comment

by:ThG
ID: 17086244
>        $this->properties['pkValue'] = $NOTE_THIS_VARIABLE;
>         $obj = Factory::createObject('B');        // NOTE: PROBLEM STATE 1 ; Go to Factory class.
> but it doesn't work. I think the reason is that a new object instance is created in Factory class. Line:

so why don't you set it after object creation? like:

$obj = Factory::createObject('B');        // NOTE: PROBLEM STATE 1 ; Go to Factory class.
$obj->properties['pkValue'] = $NOTE_THIS_VARIABLE;

it is unelegant, but there are other coding style problems in your code, so that's not a really matter..
0
 

Author Comment

by:karthikramanathan
ID: 17179956
Thank you for participation, but I am no longer in this task.
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 17220269
Closed, no points refunded.
Netminder
Site Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now