By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,724 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,724 IT Pros & Developers. It's quick & easy.

Class problem

P: n/a
consider this:

?

class A {

function A() {
$b = 'hello world';
}

function B() {
$c = new C();
$c->doStuff();
}

}

class C {

function C() {
$blah = 0;
}

function doStuff() {
echo 'foo bar ' . A::b;
}

}

$a = new A();

$a->B();

?>

I get a parse error on line 23 with
echo 'foo bar ' . A::b;

But I have to retrieve the property $b from class A FROM the doStuff()
method in Class C...

how do I do that?

Thanx
Phil
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Sorry this is more accurate:

<?

class A {

function A() {
$graduation_month = '05';
$c = new C();
$c->doStuff();
}

}

class C {

function C() {
$blah = 0;
}

function doStuff() {

}

}

$a = new A();

$a->A();

?>

Except that doStuff() needs to get the name 'graduation_month' and the value
$graduation_month that are in A. Not sure how.

Phil
Jul 17 '05 #2

P: n/a
Ok I gave up on the class idea and decided on a simpler approach: include a
PHP script with a function that will return an array with elements being the
HTML strings I need. I however, have to have the showApplicant class
because that's been pre-existing in the original site structure since I
started working where I work at, so getting rid of it will pretty much
destroy most of the existing site. So here is something that I would want:

<?
function DropdownGenerator($monthName = '', $dayName = '', $yearName = '')
{
$blah = array("$monthName = ${$monthName}",
"$dayName = ${$dayName}",
"$yearName = ${$yearName}"
);
return $blah;
}

class showApplicant {

function showApplicant() {
$graduation_month = '05';
$graduation_year = '2001';
print_r(DropdownGenerator('graduation_month', '', 'graduation_year'));
}

}

$app = new showApplicant();
?>
However, I cannot still get the values of $graduation_month, etc. I cannot
change showApplicant class no matter what, but I can change the
DropdownGenerator function, how must I do so to make sense?

Phil
Jul 17 '05 #3

P: n/a
Phil Powell wrote:
Sorry this is more accurate:

<?

class A {

function A() {
$graduation_month = '05';
$c = new C();
$c->doStuff();
}

}

class C {

function C() {
$blah = 0;
}

function doStuff() {

}

}

$a = new A();

$a->A();

?>

Except that doStuff() needs to get the name 'graduation_month' and the value
$graduation_month that are in A. Not sure how.


What do you think function args are for ?

class C {
function doStuff($name, $value) {
// do whatever with $name and $value
}
}

class A {
function A() {
$graduation_month = '05';
$c = new C();
$c->doStuff('graduation_month', $graduation_month);
}
}

Bruno

Jul 17 '05 #4

P: n/a
Phil Powell wrote:
consider this:

?

class A {

function A() {
$b = 'hello world';
}

function B() {
$c = new C();
$c->doStuff();
}

}

class C {

function C() {
$blah = 0;
}

function doStuff() {
echo 'foo bar ' . A::b;


b is not a property of class A, it's a local variable in method A::A().
if you want b to be a property of class A, you need to declare class A
like this :

class A {
var $b = 'hello world';

// etc
}

Now by accessing b with the A::b syntax, you'll have the same value for
all and every instances of A (the value you initialized A::b with).

I think what you want is :

class C {
function doStuff($a) {
echo 'foo bar ' . $a->b;
}
}

class A {
var b = '';

function A() {
$this->b = 'baaz';
}

function fun() {
$c = new C();
$c->doStuff($this);
}
}
HTH
Bruno

Jul 17 '05 #5

P: n/a
Den Thu, 16 Oct 2003 01:18:45 -0400. skrev Phil Powell:

However, I cannot still get the values of $graduation_month, etc. I
cannot change showApplicant class no matter what, but I can change the
DropdownGenerator function, how must I do so to make sense?

Why not:
class DropdownGenerator {
var $month = array(
'0' => 'January',
'1' => 'February',
etc..
);
var $day = array(
'0' => 'Monday',
'1' => 'Tuesday',
etc.
);
var $year = array(
'0' => 'xxxx',
'1' => 'yyyy',
etc.
);
var $blah;

function getDropdownGenerator($monthName, $dayName, $yearName) {
$this->blah = array(
$monthName => $month->$monthName,
$dayName => $day->$dayName,
$yearName => $year->$yearName
);
return $this->blah;
}
}

class ShowApplicant extends DropdownGenerator {

var $graduation_month;
var $graduation_year;
var $graduation_day;

function showApplicant($d = '', $m = '', $y = '') {
$this->graduation_month = $m;
$this->graduation_year = $y;
$this->graduation_day = $d;
}

function setOptions($d, $m, $y) {
$this->graduation_month = $m;
$this->graduation_year = $y;
$this->graduation_day = $d;
}

function getPrint() {
return getDropdownGenerator(
$this->graduation_month,
$this->graduation_day,
$this->graduation_year
);
}
}

$obj = &new ShowApplicant();
$obj->setOptions('1','1','2003');
print_r($obj->getPrint());

Ontested!

--
Hilsen/Sincerely, Michael Rasmussen

En windows admin er en person, for hvem den stÝrste bedrift er, at
lave konfiguration af serveren med trial and error via en gui.

Jul 17 '05 #6

P: n/a
I'm sorry, I understood none of what you said. Je ne comprends pas
tout ce que tu as ecrit a moi.

In fact, I wound up with a solution on my own involving variable
bundling and passing collections into the class instead.
Phil
Bruno Desthuilliers <bd***********@removeme.free.fr> wrote in message news:<3f***********************@news.free.fr>...
Phil Powell wrote:
consider this:

?

class A {

function A() {
$b = 'hello world';
}

function B() {
$c = new C();
$c->doStuff();
}

}

class C {

function C() {
$blah = 0;
}

function doStuff() {
echo 'foo bar ' . A::b;


b is not a property of class A, it's a local variable in method A::A().
if you want b to be a property of class A, you need to declare class A
like this :

class A {
var $b = 'hello world';

// etc
}

Now by accessing b with the A::b syntax, you'll have the same value for
all and every instances of A (the value you initialized A::b with).

I think what you want is :

class C {
function doStuff($a) {
echo 'foo bar ' . $a->b;
}
}

class A {
var b = '';

function A() {
$this->b = 'baaz';
}

function fun() {
$c = new C();
$c->doStuff($this);
}
}
HTH
Bruno

Jul 17 '05 #7

P: n/a
Sorry I can't do that. If I rewrote showApplicant like that nearly
the entire prewritten site I inherited would fail because
showApplicant has to be standalone.

What I wound up doing was variable bundling into a new class I wrote,
DateGroupHTMLGenerator and handling it that way, works perfectly,
except that there is now some bug involving array_search and
array_keys (more on that in another post).
Thanx though

Phil

Michael Rasmussen <mi*@datanom.net> wrote in message news:<pa****************************@datanom.net>. ..
Den Thu, 16 Oct 2003 01:18:45 -0400. skrev Phil Powell:

However, I cannot still get the values of $graduation_month, etc. I
cannot change showApplicant class no matter what, but I can change the
DropdownGenerator function, how must I do so to make sense?

Why not:
class DropdownGenerator {
var $month = array(
'0' => 'January',
'1' => 'February',
etc..
);
var $day = array(
'0' => 'Monday',
'1' => 'Tuesday',
etc.
);
var $year = array(
'0' => 'xxxx',
'1' => 'yyyy',
etc.
);
var $blah;

function getDropdownGenerator($monthName, $dayName, $yearName) {
$this->blah = array(
$monthName => $month->$monthName,
$dayName => $day->$dayName,
$yearName => $year->$yearName
);
return $this->blah;
}
}

class ShowApplicant extends DropdownGenerator {

var $graduation_month;
var $graduation_year;
var $graduation_day;

function showApplicant($d = '', $m = '', $y = '') {
$this->graduation_month = $m;
$this->graduation_year = $y;
$this->graduation_day = $d;
}

function setOptions($d, $m, $y) {
$this->graduation_month = $m;
$this->graduation_year = $y;
$this->graduation_day = $d;
}

function getPrint() {
return getDropdownGenerator(
$this->graduation_month,
$this->graduation_day,
$this->graduation_year
);
}
}

$obj = &new ShowApplicant();
$obj->setOptions('1','1','2003');
print_r($obj->getPrint());

Ontested!

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.