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.
karthikramanathanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

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
Giovanni GCTOCommented:
>        $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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.