Hi,
I have just started learning OOP
and have a couple of questions.
Is it correct to say that if I write a constructor method for my class
then I will not need to use he set method to add values to properties ?
i.e. with this: -
<?php
-
class person {
-
var $name;
-
var $sp_number;
-
public $height;
-
protected $social_insurance;
-
-
function __construct($persons_name) {
-
$this->name = $persons_name;
-
}
-
-
function get_name() {
-
return $this->name;
-
}
-
-
} // end class
-
?>
-
this will not be needed: - function set_name($new_name) {
-
$this->name = $new_name;
-
}
-
because in my html I will use this: - <?php
-
$stefan = new person("Stefan Mischook");
-
-
echo "Stefan's full name: " . $stefan->get_name()."<br>";
-
?>
True - or have I got things mixed up ?
9 3202 @jeddiki
You may later want to change the person's name. Maybe he/she gets married and take's the other person's name? Should always have getter and setter methods, I believe.
Also, could you please read your Personal Messages.
Thanks,
Markus.
Following on from that question is another
basic newbie one:
Again using this class: class_lib.php -
class person {
-
var $name;
-
var $sp_number;
-
public $height;
-
protected $social_insurance;
-
-
-
function __construct($persons_name) {
-
$this->name = $persons_name;
-
}
-
-
-
function set_name($new_name) {
-
$this->name = $new_name;
-
}
-
-
function get_name() {
-
return $this->name;
-
}
-
-
} // end class
-
To apply values to the name property I use this:
<?php
$stefan = new person("Stefan Mischook");
?>
How do I add a value to the $sp_number ?
I guess that I can not use the constructor again
so do I add this to class_lib.php ? - function set_sp_number($new_sp_number) {
-
$this->sp_number = $new_sp_number;
-
}
-
and then use it like this ? -
$stefan->set_sp_number(8872);
Any explaination would be greatly appreciated :)
You can pass multiple arguments to a method. -
-
class Example
-
{
-
-
public $firstName;
-
public $lastName;
-
-
// Constructor
-
function Example($first, $last)
-
{
-
$this->firstName = $first;
-
$this->lastName = $last;
-
}
-
-
}
-
-
$ex = new Example("John", "Dorian");
-
-
Thats fine but what if I need to add the $lastName at a
later date ( eg when they fill out a profile form).
How do I add the $lastName then - I guess a should not
use : - $ex = new Example( , "Dorian");
or could I ?
You can set default values for arguments on methods. So you can provide either a first name and a last name, or just a first name. -
function Example($first, $last = "")
-
{
-
....
-
}
-
And you could set the last name later, with a setter method. -
$ex->set_lastName("Dorian");
-
You don't call your constructor after you have initialised the class, i.e after you have done $ex = new Example().
@jeddiki
correct, PHP will throw an error in this case.
to add the name either use the setter method or set the property directly, if it is defined public. - $ex->lastName = "Dorian";
note: the var keyword is not encouraged in PHP 5, it is simply kept for compatibility reasons towards PHP 4
Do you mean the var that I put in my class_lib.php ? -
<?php
-
-
class person {
-
var $name;
-
var $sp_number;
-
public $height;
-
protected $social_insurance;
-
If so how should I be writing it in that class definition ?
BTW I am reading through the PHP 5 Power Programming by Andi Gutmans
so hopefully I'll get better at this ;)
@jeddiki
a) yes
b) that depends on the usage, possible values - public (external read/write access)
- protected (internal access across inheritance)
- private (access only inside the defining class)
static, const and read-only are covered later
You should declare variables as protected, private or public (usually).
So don't use the 'var' keyword, but any of the above. It's not going to give you any trouble, though.
-_- nvm.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Raymond Hettinger |
last post by:
I would like to get everyone's thoughts on two new dictionary methods:
def count(self, value, qty=1):
try:
self += qty
except KeyError:
self = qty
def appendlist(self, key, *values):
try:
|
by: Jimmy Cerra |
last post by:
I am a little confused how the memory for objects is allocated in
JavaScript. David Flanagan, in "JavaScript: The Definitive Guide,"
states that each property of a class takes up memory space when...
|
by: Flipje |
last post by:
In my view, there is a major drawback to using attributes: the getter and
the setter have identical protection levels. But I usually want the getter
to be public and the setter to be protected or...
|
by: Herve Bocuse |
last post by:
Hi,
I'm just wondering what are the guidelines for using a Property or a pair of
get/set methods related to a member variable ?
What do yu guys do ?
Thank you
Herve
|
by: John |
last post by:
Trying to find out what is essential / optional, I made an extremely simple
Class and Module combination to add two numbers. (see below)
It appears that an empty constructor is needed n order to...
|
by: Andrew Poulos |
last post by:
With the following code I can't understand why this.num keeps
incrementing each time I create a new instance of Foo. For each instance
I'm expecting this.num to alert as 1 but keeps incrementing.
...
|
by: rickeringill |
last post by:
Hi comp.lang.javascript,
I'm throwing this in for discussion. First up I don't claim to be any
sort of authority on the ecmascript language spec - in fact I'm a
relative newb to these more...
|
by: Martin Pöpping |
last post by:
Hello,
I´m coming from the Java World. Here Programmers often use (like in
C++?) getter and setter methods.
F.e.:
class Mirror{
private int width_;
|
by: Cliff Williams |
last post by:
Can someone explain the pros/cons of these different ways of creating a
class?
// 1
function myclass() {
this.foo1 = function() {...}
}
// 2a
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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...
| |