473,574 Members | 2,579 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 6284
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
3584
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
1575
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
1219
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
2096
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
3464
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
2670
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
3336
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...
7
10488
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
3357
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...
6
2180
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...
0
7807
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7730
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...
0
8058
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8100
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...
0
5301
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...
0
3742
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2249
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.