473,508 Members | 2,490 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 6275
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*******@attglobal.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
3575
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...
5
1569
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
1215
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...
8
2085
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...
10
3454
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
2662
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
3329
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...
7
10477
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...
7
3353
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...
6
2167
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...
0
7231
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
7336
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,...
0
7405
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...
1
7066
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
7504
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...
0
4724
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...
0
3214
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...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
435
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...

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.