473,695 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing variables to a class

Greetings, how would I pass variables that are outside a class ... to
a class? Such as ...

$variable_one = 1;
$variable_two = 2;

class this_is_a_class {

public $variable_one;
public $variable_two;

}

I want to pass both variables to the class this_is_a_class . How would
I go about doing this?
Sep 19 '08 #1
3 6297
vern wrote:
Greetings, how would I pass variables that are outside a class ... to
a class? Such as ...

$variable_one = 1;
$variable_two = 2;

class this_is_a_class {

public $variable_one;
public $variable_two;

}

I want to pass both variables to the class this_is_a_class . How would
I go about doing this?
One way is to use setter functions.

Study this and you will begin to grasp a basic understanding of Classes.

http://www.php.net/manual/en/language.oop5.php
Sep 19 '08 #2
vern wrote:
Greetings, how would I pass variables that are outside a class ... to
a class? Such as ...

$variable_one = 1;
$variable_two = 2;

class this_is_a_class {

public $variable_one;
public $variable_two;

}

I want to pass both variables to the class this_is_a_class . How would
I go about doing this?
Setter functions and/or a constructor.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Sep 19 '08 #3
vern wrote:
Greetings, how would I pass variables that are outside a class ... to
a class? Such as ...

$variable_one = 1;
$variable_two = 2;

class this_is_a_class {

public $variable_one;
public $variable_two;

}

I want to pass both variables to the class this_is_a_class . How would
I go about doing this?
The whole idea behind OOP is that you can't do that. All you can do is
to have the instance of the class obtain the value on its own. This is
done with a mutator function where the instance of the class goes and
obtains the value and then sets that value in its own data set. Your
example:

Inside the class TheClass:

private $variable_one;
private $variable_two;

public function setVariableOne( $val) {
$this->variable_one = $val;
}

Outside the class TheClass;

$obj = new TheClass();
$obj->setVariableOne ('foo');

The variables $variable_one and $variable_two are members of the class
and are NOT exposed to the outside world except my mutator functions of
set and get. You can't just set them or get them by a direct call. You
MUST have those helper mutator functions.
Sep 19 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3596
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back in my variables ?? note1: I'm not the one who wrote the web service.. so I can't modify
5
1577
by: Michael Sgier | last post by:
Hello i don't understand the visibility/passing of variables. I've in texture.h: class CTexture { public: unsigned int texID; GLuint texture;
1
1225
by: Reinier Beeckman | last post by:
Hi, In a program i'm working on i got several classes. 3 of them have relation to my problem. Let's name them classes class A, class B and class C. classA { public static ClassA A = new ClassA( ); public static void Main()
8
2109
by: JJ | last post by:
Hi, What's the preferred way to pass variables around to different pages now? Or if my reading servers me right they are retained in memory for the life of the app, correct? How do I access these variables if in a different page than the one variable was created in? Thanks, JJ
10
3469
by: Stan | last post by:
There are two ways to pass structured data to a web service: xml === <Order OrderId="123" OrderAmount="234" /> or class =====
12
2679
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
9
3341
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my limited c & c++ experience. Maybe some of you can help. the problem: I need several instances of a class whose (non-static!) methods should serve as callbacks for a dll (which can' be manipulated/adapted in any
7
10492
by: andy | last post by:
A question about about passing a class by reference: Say you have a class called car, and within that you have two objects called car01 and car02. Within the class I have an int variable called wheels. I have declared the following: int number_of_wheels( const car &new_wheels)
7
3361
by: amygdala | last post by:
Hi all, I'm starting this new project in which I'ld like to implement sort of a design pattern I have seen being used in the CodeIgniter framework. Basically, the site will examine the URI and based on the segments of the URI it will fire up some controller class, for instance, say I have an inbox in which end-users can view messages they got from other users, they'ld start at:
6
2192
BezerkRogue
by: BezerkRogue | last post by:
This is the most fundamental action I am sure, but I can't seem to make it happen. I am familiar with passing variables in ASP. But that doesn't seem to be the preferred method in .NET. I have some scripts that run at the server for the form so I can't disable that statement. I followed Microsoft's instructions(I know..not the best thing to do) but still can't get the variables to pass. I created a class and set up an @reference...
0
8565
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8977
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8839
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6488
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5837
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2269
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.