PHP, OOP, Creating an object [closed]

2019-06-13 16:23发布

I am Studying PHP, OOP and i have learned a lot, but there is small things who tease me,

There is an image I created for illustration of object creation:Link

if someone could answer a few questions please...

  • I will be happy if some one can correct me if i did something wrong with the image.
  • When i try to echo an object i getting an error that say that the Object (i understand that the Object is the memory reference) cannot be converted to a string, if its not a string of the memory location name(example: 'F70') can i have an example please for how does memory location looks like.
  • And about the variable containing the reference, i understand that this is a regular variable placed with all the rest of the global variables, i mean that if i create new object and a new variable the first object(variable) contain the reference but it is a regular global variable, same is the other variable:

First Step: i define the object variable...

Second Step: the Object created and it memory location getting inside the variable we wanted...

Third Step: New global variable created with our reference.

What mean that when i call a Object(variable):

First Step: We are going to the memory location where our variable created...

Second Step: The Variable value(reference) referring us to the memory location where the object is...

Third Step: We have access to methods and properties of that object.

I will be happy if some one can help me understand this things, thank you all and have a nice day.

标签: php oop
5条回答
做个烂人
2楼-- · 2019-06-13 16:29

I am not a PHP programmer, but maybe I can help you at least a bit to lower your confusion.

Again, I am not sure how php (since it is interpreted language) actually handles object creation, but it should be very close to the way C++ does it. In the end of the day, in runs on the same HW.

So, you know your class is a "template" for compiler (interpreter). But you need to understand what object actually is. Object is in fact like a table of information. It contains your class´s variables. It does not contain methods.

So, when you create object using new operator, this "table" is created in memory and filled with values. This table has its base address. That is assigned to the object variable.

Important thing here is, object does not contain actuall methods. These are created elsewhere in memory. When you call some object method, you actually call "regular" function, and compiler passes the object´s (that table´s) address to it as one its parameters.

This is very simplified. But I thing it should help you image, what computer actually does in the background.

查看更多
太酷不给撩
3楼-- · 2019-06-13 16:31

All PHP variables are stored as "zval".

More info about zval's: http://docstore.mik.ua/orelly/webprog/php/ch14_06.htm

Function to dump the internal info of a zval: http://php.net/manual/en/function.debug-zval-dump.php

查看更多
一夜七次
4楼-- · 2019-06-13 16:40
  1. To have a string representation of an object you have to implement function __toString()

  2. Regarding internal mechanisms - its a lot lot more complicated than you think. As PHP is a dynamic language - it haas to keep a lot of information for every class, for every class object. Not just memory needed for string variable, but also function mapping, memory management structures, types structures, defaults.. If you want to get deeper, read this

查看更多
闹够了就滚
5楼-- · 2019-06-13 16:44

When you try to echo your object, you have to override the __toString() magic function in your class. I really advice you to read this article http://net.tutsplus.com/tutorials/php/object-oriented-php-for-beginners/. It pretty much answers your questions.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-06-13 16:51

PHP does not return the memory reference but an instance of your class. In general it is unknown to you where in the memory the object is created and stored. If you still need to work with memory locations and references in PHP then there is already a great answer on Stack Overflow: How check memory location of variable in php?. Regarding the Usage of OOP in PHP you should check out the Classes and Objects manual on php.net.

Regarding echoing an object: If you want to echo an object, you need to define a __toString() magic method that specifies how the object should be represented as string. In your case this would be

<?php
class x {
    public function num() {
         return 5;
    }
    public function __toString() {
         return $this->num();
    }

}
$ob = new x();
echo $ob; // outputs 5

In PHP, $ob is not a reference to a memory location, but an instance of you class. So you could also call echo $ob->num(); without defining a __toString() method.

查看更多
登录 后发表回答