Jump to content

Object Oriented PHP language rules


danludwig

Recommended Posts

Hi,

 

I am trying to decipher some OO PHP code, and don't have PHP environment to test in. I've had some experience with PHP years ago, but very little of it was using OO PHP. If anyone could help answer any of the following questions for me, I'd appreciate it.

 

1.) From what I remember, the $this variable refers to the class calling a method or field, using the syntax $this->_someField or $this->someMethod. What does it mean if a $this field reference is expressed in a method, without declaring it in the actual containing class? For example:

 

class MyPhpClass

{

    function myMethod() {

        $this->variable1 = array();

        $this->variable2 = array();

        $methodLocalVariable1 = null;

        $methodLocalVariable2 = 30;

    }

}

 

The class fields variable1 and variable2 are not declared in the class (i.e. there is no "var $variable1, $variable2;" line). Does expressing them in the method atuomatically declare them for use by the entire class? In other words, is this a shortcut for using class-scoped fields without declaring them explicitly outside of the method body?

 

2.) I have actually read the PHP Manual regarding this next question, but I'm still confused. Say I have the following in a method:

 

$this->array1 = array();

$this->array2 = array();

$this->currentArray =& $this->array1;

 

Does the use of the amphersand (&) after the assignment operator (=) ensure that I'm storing a reference to $this->array1 rather than a separate copy of it? In other words, what would be different if I removed the amphersand and just had the line "$this->currentArray = $this->array1;" instead? If I modified $this->currentArray, would it modify $this->array1 as well?

 

3.) Continuing along the lines of question #2, consider the following:

 

$this->array1 = array();

$this->array2 = array();

$this->currentArray =& $this->array1;

 

while (reading and parsing a string) {

    if (some condition is met) {

        $currentArray =& $obj;

    }

}

 

In the above lines, $currentArray is not declared in a local scope -- only $this->currentArray is declared. Also, $obj is never declared or initialized. If there is no other use of either of these variables ($obj or $currentArray without the $this pointer), then what in the world does this line of code do? I've searched the entire class' source code, and this line is the only place where either of these variables is used.

 

Thanks in advance for your help.

 

Dan

Link to comment
Share on other sites

Hi,

 

I am trying to decipher some OO PHP code, and don't have PHP environment to test in. I've had some experience with PHP years ago, but very little of it was using OO PHP. If anyone could help answer any of the following questions for me, I'd appreciate it.

 

1.) From what I remember, the $this variable refers to the class calling a method or field, using the syntax $this->_someField or $this->someMethod. What does it mean if a $this field reference is expressed in a method, without declaring it in the actual containing class? For example:

 

The 'this' keyword refers to the current object.  In your example code:

 

class MyPhpClass
{
    function myMethod() {
        $this->variable1 = array();
        $this->variable2 = array();
        $methodLocalVariable1 = null;
        $methodLocalVariable2 = 30;
    }
}

 

MyPhpClass has two data members - variable1 and variable2.  To clarify, here's a better example:

 

class Person
{
   private $name;
   private $age;

   public function __construct($name, $age)
   {
      $this->name = $name;
      $this->age = $age;
   }

   public function getName() { return $this->name; }
   public function getAge() { return $this->age; }
}

$Bob = new Person("Bob", 23);
$Martha = new Person("Martha", 66);

echo $Bob->getAge();
echo $Martha->getAge();

 

So, using the example above, you should see that Bob and Martha are two instances of the Person class.  Despite the class code being the same for each, the values of their properties are different.  Why?  Because, again, 'this' refers to the current object.  When getAge() is invoked on Martha, she is the current object being accessed.

 

Another thing to point out - variables not prefaced with 'this' in an object are temporary.  The 'this' implies that you're dealing with a data member/property of an object.  Something that is an internal part of the object.  $this->name is literally this object's data member entitled 'name.'  Furthermore, scope still applies.  A temporary variable created in a method will not be globally available to the rest of the class.

 

2.) I have actually read the PHP Manual regarding this next question, but I'm still confused. Say I have the following in a method:

 

$this->array1 = array();

$this->array2 = array();

$this->currentArray =& $this->array1;

 

Does the use of the amphersand (&) after the assignment operator (=) ensure that I'm storing a reference to $this->array1 rather than a separate copy of it? In other words, what would be different if I removed the amphersand and just had the line "$this->currentArray = $this->array1;" instead? If I modified $this->currentArray, would it modify $this->array1 as well?

 

Yes, the '&' means a reference.  In your code example, $this->currentArray holds a reference to $this->array1.  With a reference, if you accessed currentArray like so:

 

$this->currentArray[0] = "Hi, folks";

 

That change would happen in array1 as well.  Why?  Because currentArray isn't an array, it's an array reference.  Anything you do to it actually happens on the array it's referencing.

 

Normal assignment (value assignment) stores a copy of the data rather than a reference.  So:

 

$this->currentArray = $this->array1;
$this->currentArray[0] = "Hi, folks";

 

array1 will remain unchanged.  Why?  Because currentArray contains a copy of array1's data, not a reference to it.

 

3.) Continuing along the lines of question #2, consider the following:

 

$this->array1 = array();

$this->array2 = array();

$this->currentArray =& $this->array1;

 

while (reading and parsing a string) {

    if (some condition is met) {

        $currentArray =& $obj;

    }

}

 

In the above lines, $currentArray is not declared in a local scope -- only $this->currentArray is declared. Also, $obj is never declared or initialized. If there is no other use of either of these variables ($obj or $currentArray without the $this pointer), then what in the world does this line of code do? I've searched the entire class' source code, and this line is the only place where either of these variables is used.

 

Thanks in advance for your help.

 

Dan

 

Wait, this is preexisting code?  Mind sharing the whole thing?

Link to comment
Share on other sites

Wait, this is preexisting code?  Mind sharing the whole thing?

 

Yes, this is preexisting code. I was just thinking maybe I should post asking for people to collaborate with me on what I'm doing than posting 1001 PHP questions.

 

What I am doing is creating an adapter for the Moxiecode TinyMCE spellchecker plugin, so that it can consume a .NET spellchecking service instead of a PHP spellchecking service. I plan to contribute my source back to Moxiecode, and to anyone interested in collaborating feel free to let me know.

 

As for the source, I don't know if I can actually upload it or post it here, but it is open source. You can download the files I'm trying to decipher from http://sourceforge.net/projects/tinymce/files/TinyMCE%20Spellchecker%20PHP/2.0.2/tinymce_spellchecker_php_2_0_2.zip/download. The files I have open right now are config.php, rpc.php, and JSON.php. The first 2 are in the root directory of the download, the third is in the classes/utils folder.

 

The source I've mentioned here is based on code from the JSON.php file, mainly in the decode readValue method of the Moxiecode_JSON class.

 

By the way, sorry about not being clear in the first question. Yes, I meant that $this points to an instance of a class (object), not the class itself. I am an OO developer, but I'm used to more strongly-typed constructs like those in Java and C#. Haven't coded PHP since about 2003.

 

Thanks for the 2 answers. Do I move on to round 3 now?

Link to comment
Share on other sites

Is there a more recent version of TinyMCE?  The code you linked me to appears to be written in PHP 4, which is pretty gimped in terms of its OO capabilities.  PHP 5 is much nicer - abstract classes, data member permission levels (public, protected, private), interfaces, the ability to declare methods as final, etc.  I wouldn't be surprised if there isn't a modernized version, given the tenacity of legacy apps, but, still, PHP 5 has been around for 5 years now, so it may be worth a look.

 

Regarding #3, I think that the code is attempting to dynamically create data members.  It's kinda sloppy, though, and definitely not the way I'd do it (I have a bit of C++ background, and am currently learning C# myself... I like to declare my members before attempting to use them).  That's the only thing I can think of, though, given the lack of declared data members and the empty constructor.

Link to comment
Share on other sites

The link I gave you was from the TinyMCE download page, http://tinymce.moxiecode.com/download.php. The software is mostly client-side javascript that runs in the browser, but the spellcheck does a server-side call, and in turn the server uses either PSpell, ASpell, or Google Spellcheck service.

 

I totally hear you on the gimpiness. I found this comment in the Moxiecode JSON.php file: "This method was needed since PHP is crapy and doesn't have pointers/references". The funnniest part is that they misspelled crappy! Anyway, this is why I abandoned PHP and MySQL 6 years ago, I was tired of waiting for them to become fully-featured OO language / RDBMS.

 

Since you say you know a little C#, I'm going to upload the classes I've been working on. These are my attempts to recode the 2 classes in the JSON.php file. The classes can be tested and debugged like so:

 

public void TestJsonParser()

{

    string sampleJsonRequest = "{\"id\":\"c0\",\"method\":\"checkWords\",\"params\":[\"en\",[\"Here\",\"is\",\"some\",\"text\",\"I\",\"want\",\"to\",\"spellcheck\"]]}"; // this is a JSON request as the server receives it from TinyMCE

    JsonGraph graph = new JsonGraph();

    object jsonData = graph.Decode(sampleJsonRequest);

}

 

So far, the JsonReader seems to be working except that it was throwing OutOfRange exceptions when it gets to the end of the string it's parsing. The JsonGraph also seems to be working, sorta, but I'm confused about the data objects it's creating. I probably did something wrong, and it's probably due to my misunderstanding of how PHP is using arrays to construct the objects.

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.