• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

PHP5 - OOP Question

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
karthikramanathan
Asked:
karthikramanathan
1 Solution
 
RoonaanCommented:
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
 
karthikramanathanAuthor Commented:
>> 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
 
RoonaanCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
karthikramanathanAuthor Commented:
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
 
ThGCommented:
>        $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
 
karthikramanathanAuthor Commented:
Thank you for participation, but I am no longer in this task.
0
 
NetminderCommented:
Closed, no points refunded.
Netminder
Site Admin
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now