Jump to content

please could you comment on my comments OOP beginner!


wright67uk

Recommended Posts

I'm trying to get my head around oop and wondered if anyone would look at my comments so far .

I want to learn a bit about constructors next but thought I should make sure I understand the below code properly first.

 

<?php
class Dictionary {
//new class

public $translations = array();
    public $type =" ";
//new variables called 'PROPERTIES'

function summarize() { 
//function within a class is called a 'METHOD'
        $ret  = "Dictionary type: {$this->type}\n"; 
	// {$this->type} means: use $type which is found within THIS class
        $ret .= "Terms: ".count( $this->translations )."\n";  
	// {$this->type} means: count the length of the $translation array which is found within THIS class
        return $ret;
    }
}
?>

 

<?php
include('class_dictionary.php'); 
//include file that holds the class

$english = new Dictionary;   // creating new object using the class called Dictionary
$english->type = "EN";       // adding a value to the 'type' property located within the dictionary class
$english->translations['TREE'] = "branch";  //adding TREE key and value branch to the translations array
$english->translations['TREEtwo'] = "trunk"; //adding TREEtwo key and value trunk to the translations array

$french = new Dictionary;  //creating a new instance of the Dictionary object
$french->type = "FR";
$french->translations['TREE'] = "arbre";

print_r( $english ); 

echo "<br/>";
print $english->summarize();  //prints the summarize method
?>

Link to comment
Share on other sites

The var $french will over write the $english there... if you use the clone operator you will avoid this.

 

 

I think this is what you meant??

<?php
include('class_dictionary.php'); 
//include file that holds the class

$english = new Dictionary; 
$english->type = "EN";       
$english->translations['TREE'] = "branch";

//start the clone here
$french = clone $english; 
$french->type = "FR";
$french->translations['TREE'] = "arbre";

print $english->summarize();  //this will print the value from english

print $french->summarize();  //this will print the value from french
?>

 

If you don't clone as above the output would be both the french inputs... and too much french is never a good thing  :P

Link to comment
Share on other sites

@wright67uk, you're on the right track.  From a design standpoint, however, you'll be better off labeling your properties as private and accessing them through public methods where necessary.  One of the main points of OOP is encapsulation.  Data should be encapsulated within an object, accessed from the outside through clear and explicit means.  Leaving properties public destroys encapsulation, allowing them to be changed at a whim.

Link to comment
Share on other sites

To end up like this?

 

<?php
class Dictionary {
//new class

public $translations = array();
    private $type =" ";

//new variables called 'PROPERTIES'

  public setBar($type) 
  {
    $this->type = $type;
  }

  public getBar()
  {
    return $this->type;
  }

function summarize() { 
//function within a class is called a 'METHOD'
        $ret  = "Dictionary type: {$this->type}\n"; 
	// {$this->type} means: use $type which is found within THIS class
        $ret .= "Terms: ".count( $this->translations )."\n";  
	// {$this->type} means: count the length of the $translation array which is found within THIS class
        return $ret;
    }
}
?>

 

sorry if im not getting this!

Link to comment
Share on other sites

As your post was to comment on your comments, all I'd suggest is maybe including a doc block for each class with the declarations in it.  I've just started looking into the OOP side of things myself and one of the things I have come accross is that, in some API implimentations, these doc blocks are really rather important for some DI patterns.  One of the newer (newest?) ZEND releases can be coded to build refferences automaticly using the doc blocks as a kind of catalogue (that's my interpretation anyway, but like I said I'm new to this OOP thing too) for building the DI relations.

 

I suck at OOP, having just started playing with it and coming from more than a few years in the procedural world, so I can't really say more than that.  I'm sure one of the better programmers will corect what I've got wrong and clear up the bit's I didn't.

Link to comment
Share on other sites

dictionary.php

<?php
include('class_dictionary.php'); 
//include file that holds the class

$english = new Dictionary;   // creating new object using the class called Dictionary
$english->type = "EN";       // adding a value to the 'type' property located within the dictionary class
$english->translations['TREE'] = "branch";  //adding TREE key and value branch to the translations array
$english->translations['TREEtwo'] = "trunk"; //adding TREEtwo key and value trunk to the translations array

$french = new Dictionary;  //creating a new instance of the Dictionary object
$french->type = "FR";
$french->translations['TREE'] = "arbre";

print_r( $english ); 

echo "<br/>";
print $english->summarize();  //prints the summarize method
?>

 

class_dictionary.php

<?php
class Dictionary {
//new class

public $translations = array();
    private $type =" ";

//new variables called 'PROPERTIES'

  public setType($type) 
  {
    $this->type = $type;
  }

  public getType()
  {
    return $this->type;
  }

function summarize() { 
//function within a class is called a 'METHOD'
        $ret  = "Dictionary type: {$this->type}\n"; 
	// {$this->type} means: use $type which is found within THIS class
        $ret .= "Terms: ".count( $this->translations )."\n";  
	// {$this->type} means: count the length of the $translation array which is found within THIS class
        return $ret;
    }
}
?>

Link to comment
Share on other sites

Haha. My fault. Your missing the function keyword (as was I in my original example).

 

  public function setType($type) 
  {
    $this->type = $type;
  }

  public function getType()
  {
    return $this->type;
  }

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.