473,394 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Class problem

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
7 1745
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steve | last post by:
Hello, I'm encountering an unexpected behavior when using the "new" modifier in a derived class to hide an inherited base class property. I use "new" intentionally so I can change the Type of...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
21
by: Mark Broadbent | last post by:
Consider the following statements //------- Item i = Basket.Items; //indexer is used to return instance of class Item Basket.Items.Remove(); //method on class item is fired item i = new...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
8
by: tshad | last post by:
I cannot seem to get the asp:textbox to use classes. Style works fine. I am trying to set the textbox to act like a label in some instance so it doesn't have a border, readonly and the background...
4
by: Tony Johansson | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. Here we have a class called B One project...
5
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
3
by: TamusJRoyce | last post by:
Hello. This is my first thread here. My problem has probably been came across by a lot of people, but tutorials and things I've seen don't address it (usually too basic). My problem is that I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.