473,396 Members | 2,099 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.

object default value in constructor

I'm trying to write a class with a constructor that takes a single object
parameter. So far so good, but I can't for the life of me make the parameter
optional. I understand how to do it with a numerical or textual parameter,
but not with an object. I tried null, but it doesn't work.

class A
{
}

class B
{
function B($a = ???)
{
if (!isset($a)) $a = new A;
}
}

Any ideas?

Jaka
Jul 17 '05 #1
4 5314
"Janez Cerar" <ja*********@email.removethis.si> wrote
in message news:<9K*******************@news.siol.net>...

I'm trying to write a class with a constructor that takes a single object
parameter.
Why? Isn't it easier to just extend the class?
So far so good, but I can't for the life of me make the parameter
optional.

class A
{
}

class B
{
function B($a = ???)
{
if (!isset($a)) $a = new A;
}
}


Try this instead:

class A {
function A () {}
}
class B extends A {}

Since you defined no constructor for B, new B will simply call the
constructor for A (in PHP 4, at least).

Cheers,
NC
Jul 17 '05 #2
With total disregard for any kind of safety measures nc@iname.com
(Nikolai Chuvakhin) leapt forth and uttered:
Since you defined no constructor for B, new B will simply call
the constructor for A (in PHP 4, at least).


Not strictly true. The parent constructor is not called by default,
it has be called explicitly.

class ClassOne {
var $number;
function ClassOne() {
$this->number = 2;
}
}

class ClassTwo extends ClassOne {
function ClassTwo() {
# Call the parent constructor
parent::ClassOne();
}

function classMethod() {
return $this->number;
}
}

$obj =& new ClassTwo;

# Will echo '2';
echo $obj->classMethod();

--
There is no signature.....
Jul 17 '05 #3
Janez Cerar:
I'm trying to write a class with a constructor that takes a single object
parameter. So far so good, but I can't for the life of me make the
parameter optional. I understand how to do it with a numerical or textual
parameter, but not with an object. I tried null, but it doesn't work.

class A
{
}

class B
{ function B($a = null) { if(!$a) { $a = new A(); } }
}


isset() checks if a variable is set or not. A variable which is set to null
is of course set. null is equivalent to false.

André Næss
Jul 17 '05 #4
Phil Roberts <ph*****@HOLYflatnetSHIT.net> wrote
in message news:<Xn*************************@206.127.4.22>...

With total disregard for any kind of safety measures nc@iname.com
(Nikolai Chuvakhin) leapt forth and uttered:
Since you defined no constructor for B, new B will simply call
the constructor for A (in PHP 4, at least).


Not strictly true. The parent constructor is not called by default,
it has be called explicitly.


The parent constructor IS called by default, if the child class
has no constructor defined:

...in PHP 4... If a class has no constructor, the constructor
of the base class is being called, if it exists.

http://php.net/manual/en/language.oop.constructor.php

This is really easy to test:

class A {
var $one;
var $two;
function A () {
$this->one = 'String One';
$this->two = 'Sting Two';
}
}

class B extends A {
function show () {
echo 'Field one = ', $this->one, "\r\n";
echo 'Field two = ', $this->two, "\r\n";
}
}

$b = new B();
$b->show();

The above code snippet prints:

Field one = String One
Field two = Sting Two

Cheers,
NC
Jul 17 '05 #5

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

Similar topics

21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
5
by: August1 | last post by:
This is a short program that I have written from a text that demonstrates a class object variable created on the stack memory and another class object variable created on the heap memory. By way...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
7
by: xllx.relient.xllx | last post by:
Q1: What happens when you apply parenthesis to the class type name? Is the constructor for the class called and returns an object?, or what? in main...: MyClass(); // end
7
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
14
by: Summercool | last post by:
The meaning of a = b in object oriented languages. ==================================================== I just want to confirm that in OOP, if a is an object, then b = a is only copying the...
7
by: Andy B | last post by:
I have an instance of an object that needs to be accessed by all members of a page like page_load, button_click events and so on. Where in the codebehind would I put the creation of the object...
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: 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: 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
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
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
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.