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

Getting $this in __constructor

105 100+
Maybe I'm missing some detail here, but I dont quite see the logic of not being able to access $this from within the constructor without instanciating another object. Doesn't the constructor run AFTER the object has been created?

[PHP]
<?php

class myClass
{
public $who;

public function __construct()
{
print ($this->who); //why is $this->name not accessable?
exit;
}

}



$object = new myClass;
$object->who = "John Doe";


?>
[/PHP]



Any help appreciated
Sep 20 '07 #1
4 3030
code green
1,726 Expert 1GB
The constructor function is called when an object is instantiated.
Here is where one generally initialises variables, not access them.
Member varables may be public but they should strictly be changed only by member functions.
[PHP]class myClass
{
private $who;

public function __construct()
{
$this->who = '';
}

public function setName($value)
{
$this->who = $value;
}
}
$object = new myClass;
$object->setName("John Doe");[/PHP]
Sep 20 '07 #2
pbmods
5,821 Expert 4TB
Heya, Aktar.

At the time that you are calling myClass::__construct(), myClass::$who has no value.
Sep 20 '07 #3
aktar
105 100+
That makes sence

Thanks guys
Sep 22 '07 #4
pbmods
5,821 Expert 4TB
Heya, Aktar.

Good luck with your project, and if you ever need anything, post back anytime :)
Sep 22 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: manish | last post by:
I have created a function, it gives more readability compared to the print_r function. As of print_r, it works both for array or single variable. I just want to add in it, the opton to view the...
2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
4
by: Jon | last post by:
function query($query, $return = false) { $fetch = mysql_query($query, $this->connection) or die2(mysql_error($this->connection)); if($return) return $fetch; } mysql_error(): supplied...
0
by: Si | last post by:
I'm writing an HttpModule to intecept web method calls and retrieve custom authentication information from their SOAP headers. I have the httpModules tag in web.config and that appears to be...
4
by: R.Manikandan | last post by:
Hi In my code, one string variable is subjected to contain more amount of characters. If it cross certain limit, the string content in the varabile is automatically getting truncated and i am...
3
by: wolverine | last post by:
Hi, I am injecting a javascript code into html pages and attaching some dom events to some elements via attachEvent (IE only). I just want to know that is there any chance by which my event...
7
by: alphasahoo | last post by:
Hi I am working on a program which writes the output a SQL select statements from number of source tables first to a load matrix and then writes to a load.dat file. But while writing to the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.