Solved

PHP5 - OOP Question

Posted on 2006-07-05
8
447 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

803 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