Please stop me if i am doing something wrong. It works but somehow it doesn't appear the right way to me... Look at the member function call in talks.php. Does this look right to you? Is there a better way to solve that? Thanks.
show.php
I am passing my user class by reference:
$talks = new talks($comments, $user);
talks.php:
[...]
function __construct($comments, &$user)
{
//Passing user class
$this->user = $user;
[...]
if ($this->user->is_loaded()){}
This looks a-ok to me. What problem do you see with it?
In php 5, objects are always passed by reference.
From http://www.php.net/manual/en/language.oop5.references.php:
A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.
So, you should not need the "&" operator in the parameter list of your constructor.
Related
Have to do some php coding but I am totally new to it. The question is I was trying to get a property value from an object called $mgm_member, this object is from wordpress plugin which records website's member info. First I use:
var_dump(get_object_vars($mgm_member));
The results shows that this obeject has 37 properties and all of them have values. Than I tried use
var_dump($mgm_member->phone);
The result is null. I tried many other properties but only the first property of this object have value. Can anyone help me? Thank you very much.
well I suppose if the scope of "phone" is private var_dump will not be able to access and view it, is it? In my case, I can view all properties and their values using var_dump() function but when I tried to get one property it just doesn't work. However,I can get the first property "id" using the same code
echo $mgm_member->id;
That is really weird.
As the property is 'private' you will need to make a function to access and return it.
A dump may display them but you WILL NOT be able to directly access a 'private' property.
class .... {
public function getPhone()
{
return ($this->phone);
}
}
then:
echo $mgm_member->getPhone();
var_dump(get_object_vars($mgm_member)); shows the object variables. If you are getting data with this line of code, you have data in your object.
You can access properties of a variable in your code with $mgm_member->phone - why not do so?
If you want to place the data into a variable you can use something like this:
$myVar=$mgm_member->phone;
but that defeats the purpose of OOP. Why not refer to it as exactly $mgm_members->phone all the way through your code?
Edit: As you point out, the property is private which means that only the object itself can use it. You could get around this by modifying the object, but this may be a nasty approach - it is private for a reason. Can you not use functions within the object to display the values? Alternately, is there a function you can use in the object to return you a clone of the object with different property attributes?
This may be a very dumb question, but how can I pass a parameter as a local variable in a PHP class.
e.g (this does not work, but represents what I want)
public function sql($this->sql_statement){
// do something
}
I would like the passed parameter to become the '$this->sql_statement' variable
Obviously, I could just do this, but I want to know if there is a better way:
public function sql($statement){
$this->sql_statement = $statement;
// do something
}
To the best of my knowledge, there is no better way. As your method may be called from outside the class, you essentially need to treat it as a standard setter method: set the member variable to the value of the passed parameter, just like in your second code.
In PHP, you can normally place an object in an array, like so:
class Car{}
$car = new Car();
// This runs without error
$array['vehicle'] = $car;
I have a custom MVC framework I've built, and I need the controller to get an ORM object from the model, so it can pass that to the view. So, I initialize my user object:
$user = new User(2);
Now, I want to put that user object into a $data array so it can be passed to the view:
($user->data returns an ORM object)
$array['user'] = $user->data;
The problem is, after doing this, I receive the following error:
Object of class ORM could not be converted to string
What am I doing wrong? Is there something I'm missing?
Thanks for any help in advance.
Edit: Here's what $user->data refers to, this is from the constructor of class User:
$this->data = ORM::for_table("users")->find_one($this->user_id);
(I'm using Idiorm as an ORM)
If you get an error message like:
Object of class ORM could not be converted to string
The first question you should ask is, "why does it have to be converted to a string"? An array can take a string just fine, so you can guess that $data is actually a string and PHP thinks you want to modify $data[0].
As you've seen, dynamically typed languages can leave befuddled if you aren't careful.
When your variables show suspect behavior, try to see what's actually in them using var_dump().
It's also a good idea to explicitly initialize arrays (eg: $my_array = array();) before using them.
why does this not work ?
class Test{
private $vars = array('ALL' => 0,
'ONE' => 1);
private $var = $vars['ALL']; // this does not work
function __construct(){
$this->var = $vars['ALL']; // this does work
}
}
code example here: http://codepad.org/QSjHMDij
why is the array not accessible in the statement
private $var = $vars['ALL']; // this does not work
Probably because you can't access $this during the initialization of the class prior to the constructor getting called (which is implied when you tried to do it in the definition for $var.) Some languages (like C#) will let you do it, but I think PHP is one that will not.
Neither "works" in the way you intend. You are not allowed to use variables when declaring instance members (hence the unexpected T_VARIABLE error). In the constructor you are referencing a local variable named $vars which does not exist, meaning you're setting $this->var to NULL.
Access the instance member by doing $this->vars. You can only do this in the constructor.
When declaring members (variables), you can't assign array key values of other members, it causes a parse error.
For example, you're thinking (wrong) that $vars['ALL'] is referring to your private $var - which it is not - it also causes a parse error. There's no way for PHP to know that when you say:
private $var = $vars['ALL'];
that you're actually saying "I want value of $this->vars['ALL'] to be assigned to $this->var", at least not the way you wrote it. That's why you do that from within a function, where you can easily manipulate members, such as you did from the constructor.
You should declare members, their visibility and set some default values (like you did for $var), but you shouldn't point them to other members' values, it's simply - wrong and luckily - it doesn't work :)
You're trying to assign a value to a variable which is designed to be part of an object, rather than the class. What you want is static variables.
What I think I know so far:
so $this-> is to access a function/var outside its own function/var ?
but how does $this-> know if its a function or a variable ?
why we refer to a var like this $this->data instead of this $this->$data ?
$this refers to the current object that a method has been invoked on. It knows if it's a function if there is a pair of parentheses at the end. We use the former syntax because $this->$data means look at the field whose name is $data; e.g. $this->foo if $data == 'foo'
$this is the variable referring to the object that you are currently inside. $this-> will access either a method or field in the current object.
As for why is it $this->data and not $this->$data, that's just a syntax quirk. You'd have to ask the PHP language designers. It's probably because the latter wouldn't make much sense for a method.
If this looks like Greek to you, then you may want to head over to the PHP manual's section on classes and objects and read up.
$this represents the instance of a given object, from the context of within the object.
I would say, knowing whether you're accessing a method or property is your responsibility. Read documentation. If you're calling an object method using this, it uses the expected syntax of $this->method($args); and properties (member variables) use the expected syntax of $this->var = 'value';
It's a pretty long subject, but in sort, $this is a pointer to an instance. $this->data refers to the data variable of a particular instance(this instance). It is $this->data and not $this->$data just because of convention.