473,396 Members | 1,968 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,396 software developers and data experts.

Using OO in PHP5

I'm new to OO in PHP, so maybe this is a dumb question, but I was
wondering if you can declare and use an object from within another
object?

I'd like to do something like this...

class cMyObject {
public function __construct() {
private $foo = new cOtherObject();
}

public function checkWhatever($input) {
$this->foo->methodInOtherObject("something");
}
}

This is a very simplistic example that doesn't really relate why I want
to do this, but is it possible?

If it is not possible, why does the following code not throw an error
for an unexpected appearance of the "new" keyword? Bug?

<?php
$myTest = new cObj1;
exit();

class cObj1 {
public function __construct() {
$foo = new cObj2;
}
} // cObj1

class cObj2 {
private $thing;
} // cObj2
?>

My test environment is PHP 5.0.2.

Jul 17 '05 #1
4 1336
no****@yakhair.com wrote:
I'm new to OO in PHP, so maybe this is a dumb question, but I was
wondering if you can declare and use an object from within another
object?

I'd like to do something like this...

class cMyObject {
public function __construct() {
private $foo = new cOtherObject();
}

class cMyObject {
private $foo;

public function __construct() {
$this->foo = new cOtherObject();
}
}
If it is not possible, why does the following code not throw an error
for an unexpected appearance of the "new" keyword? Bug?


It is, as long as you define the class properties the correct way.
JW

Jul 17 '05 #2
.oO(no****@yakhair.com)
I'm new to OO in PHP, so maybe this is a dumb question, but I was
wondering if you can declare and use an object from within another
object?
Sure.
I'd like to do something like this...

class cMyObject {
public function __construct() {
private $foo = new cOtherObject();
}
The variable $foo will only be valid in the context of the constructor.
To access instance variables use the $this->varName syntax.
This is a very simplistic example that doesn't really relate why I want
to do this, but is it possible?
Sure, but you should declare variables before using them (at least when
using classes):

class cMyObject {
private $foo;

function __construct() {
$this->foo = new cOtherObject();
}

...
}
If it is not possible, why does the following code not throw an error
for an unexpected appearance of the "new" keyword? Bug?


No, the 'new' keyword is not unexpected in this case. If it would be,
every assignment to a previously undeclared variable would cause an
error. Except for the assigned value there's no difference between these
statements:

$foo = 42;
$bar = new Something();

Micha
Jul 17 '05 #3
<no****@yakhair.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
I'm new to OO in PHP, so maybe this is a dumb question, but I was
wondering if you can declare and use an object from within another
object?

I'd like to do something like this...

class cMyObject {
public function __construct() {
private $foo = new cOtherObject();
}

public function checkWhatever($input) {
$this->foo->methodInOtherObject("something");
}
}

This is a very simplistic example that doesn't really relate why I want
to do this, but is it possible?


class cMyObject {
private $foo = new cOtherObject();
}

This assumes that you don't pass any parameters to $foo otherwise

class cMyObject {
private $foo = null;

function __construct($params) {
$this->foo = new cOtherObject($params);
}
}

Jul 17 '05 #4
CJ Llewellyn wrote:
class cMyObject {
private $foo = new cOtherObject();
}


This throws a parse error because of the unexpected "new" keyword and the
brackets. Only your second example is valid.
JW

Jul 17 '05 #5

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

Similar topics

8
by: Rob Ristroph | last post by:
I have tried out PHP 5 for the first time (with assistance from this group -- thanks!). The people I was working with have a site that uses lots of php objects. They are having problems with...
4
by: badbetty | last post by:
Dear Googlers I have installed PHP5 to run on WinXP against Apache 2. It works! ie. I have tested a few simple scripts and a basic xml document parse. I now want to try the XSL extension so I...
1
by: Pedro Fonseca | last post by:
Greetings everyone! I'm porting my applications to PHP5 and I've stumbled on yet another problem. I'll try to simplify things a bit. I have a main script that is being executed (index.php, PHP5...
5
by: Aziz | last post by:
Hi, I've recently contacted technical service of a web hosting company and asked them wheter or not they're gonna upgrade to PHP5 and MySQL5. Here's a quote from their response which confused me...
4
by: Chuck Anderson | last post by:
I am trying to install Php5 on my WindowsXP machine (at home, not on the 'net). I downloaded the Php Windows binary at php.net, unzipped it to c:/Php5, changed my Apache config file to use Php5...
0
by: cty | last post by:
Title: Problem in session using php5 Good day, I use php5+mySQL4+IIS5.x Previuosly i use php4 and no error occur,
19
by: McKirahan | last post by:
I am working in two environments neither configuration of which I can change; one's my Web host the other a client. My Web host requires the use of the ".php5" extension to use PHP v5.1.4; where...
3
by: xhe | last post by:
I have just upgraded my php version form php4 to php5. and I met this problem, and don't know if you know the solution. My site was written in PHP4, and most parts can be running smoothly in PHP5,...
8
by: FFMG | last post by:
Hi, I am slowly moving my code to php5. But I would like to make it backward compatible in case something bad happens, (and to make sure I understand what the changes are). The way the...
5
by: simon m | last post by:
Hi All, I have a script sitting on my web server called auto_mail.php5. I would like this to be used two different ways. One way would be for a web form to call it as submit action on a form. 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:
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.