473,399 Members | 3,106 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,399 software developers and data experts.

HELP ! ! ! something simple . . . :D

Hello all . . .

I need a help for something simple.

I have create two php classes

for example

class classname_1
{

}

and the

class classname_2
{

}

if i like to make an instance of class classname_1 into the classname_2
how can i do it ? ? ?

I have try to make one like

class classname_2
{
$obj_class1 = new classname_2();
}

but it doesn't work ! ! ! !

Why ? ? ?

Thanks a lot . . . :D

Dec 29 '06 #1
9 1151
"CorfuVBProgrammer" <me***********@gmail.comwrote in message
news:11*********************@48g2000cwx.googlegrou ps.com...
Hello all . . .

I need a help for something simple.

I have create two php classes

for example

class classname_1
{

}

and the

class classname_2
{

}

if i like to make an instance of class classname_1 into the classname_2
how can i do it ? ? ?

I have try to make one like

class classname_2
{
$obj_class1 = new classname_2();
}

but it doesn't work ! ! ! !

Why ? ? ?

Thanks a lot . . . :D
I'd do it like this:

class A {

}

class B {
private $my_object;
public function __construct(){
$this->my_object = new A();
}
}

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Dec 29 '06 #2
Thanks a lot . . .

You helped me so much . . . :D

Dec 29 '06 #3
New Question

if i like to use the instance from other method into the class how can
i do that ? ? ?

example:

class class_a
{
public function method_a()
{

}
}

class class_b
{
private $objInstance;

public function __construct()
{
$this->objInstance = new class_a();
}

public function method_t()
{
how to use the $objInstance methods into this method ? ? ?
}
}

thanks a lot

Dec 29 '06 #4

CorfuVBProgrammer a écrit :
New Question

if i like to use the instance from other method into the class how can
i do that ? ? ?

example:

class class_a
{
public function method_a()
{

}
}

class class_b
{
private $objInstance;

public function __construct()
{
$this->objInstance = new class_a();
}

public function method_t()
{
how to use the $objInstance methods into this method ? ? ?
}
}

thanks a lot
You can use objInstance methods like that :
class class_b
{
private $objInstance;

public function __construct()
{
$this->objInstance = new class_a();
}

public function method_t()
{
$this->objInstance->method();
}
}

Dec 29 '06 #5
This way it works but only into the constructor method.

I have try to use ot in other methods in the class and doesn't work ! !
! ? ? ?

Dec 29 '06 #6
This way it works but only into the constructor method.

I have try to use ot in other methods in the class and doesn't work ! !
! ? ? ?

Is any other way ? ? ?

Dec 29 '06 #7
..oO(CorfuVBProgrammer)
>This way it works but only into the constructor method.
Nope.
>I have try to use ot in other methods in the class and doesn't work ! !
! ? ? ?
Post your code.

Micha
Dec 29 '06 #8

CorfuVBProgrammer schrieb:
This way it works but only into the constructor method.

I have try to use ot in other methods in the class and doesn't work ! !
! ? ? ?

Is any other way ? ? ?

Hello!

I'm a PHP newbie, but I've been busy writing much OO-Code over the last
years.
I had an idea solving this problem using the following scheme:

class A
{
public function methodA(){
//some useful code
}
class B
{
private A $instanceOfA; //but I am really not sure if this one's works

public __construct() {
$instanceOfA=new A();
}
public function methodB(){
$this->instanceOfA->methodA();
}
}
}
Anyway this is legal in terms of OO-Programming and also a well used
method for composing classes. I tried this out in PHP but it doesn't
seem to work. Obviously you'll have to create the instance within the
function/method you want to use it. Like:

class B
{
//....
public function methodB(){
$instanceOfA=new A();
$instanceOfA->methodA();
}
}

Well I ran a test with that and it worked. It's not that elegant but I
think it makes sense...

regards
Kostas

Dec 31 '06 #9
class A
{
public function methodA(){
//some useful code
}
class B
{
private A $instanceOfA; //but I am really not sure if this one's works

public __construct() {
$instanceOfA=new A();
}
public function methodB(){
$this->instanceOfA->methodA();
}
}
}
Anyway this is legal in terms of OO-Programming and also a well used
method for composing classes. I tried this out in PHP but it doesn't
seem to work. Obviously you'll have to create the instance within the
function/method you want to use it. Like:
What you really wanted to do in the constructor of B was the following:

public __construct() {
$this->instanceOfA = new A();
}

Because with $instanceOfA you create a local (in that function) variable...
--
Tim Van Wassenhove <url:http://www.timvw.be/>
Dec 31 '06 #10

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

Similar topics

6
by: max reason | last post by:
A method in one of my classes needs to call one of 256 other methods in the same class based on an unsigned 8-bit value (0x00 to 0xFF). How is this done? Everything I try generates errors. ...
13
by: Joe Black | last post by:
Just to inform you guys that i have only like 2 weeks that i took my first classes in c++, and my proffesor now is asking me to solve this problem: /// Using a function create a Win32 Console...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
2
by: Lisa Pearlson | last post by:
Hi, My php application (on Apache/Linux) needs to do the following: The PHP script receives a request from a client (binary), asking for certain records of data. My PHP script loops through...
18
by: Jeremy Weiss | last post by:
I'm trying to build a database that will handle the monthly billing needs of a small company. I'm charting everything out and here's what I see: table for customers sub table to track payments...
7
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a...
2
by: Steve K | last post by:
I got a bit of a problem I like some help on. I'm designing an online training module for people that work in food processing plants. This is my target audience. These workers have little or no...
17
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and...
2
by: Ken Crismon | last post by:
Hello, I am currently working on an embedded systems project where by I have written a small web server that is being hosted on our internet appliance (running on an Atmega128 chip and doing...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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
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...

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.