Jump to content

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
?>

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

@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.

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!

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.

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;
    }
}
?>

thank you...  I've got the functions in place - I should of known better myself!

 

now when i run dictionary.php i get;

 

Fatal error: Cannot access private property Dictionary::$type in C:\wamp\www\dictionary.php on line 6

 

 

That is because you cannot access the $type property directly anymore, you need to access it via the new methods you have created.

 

May I suggest you take a look at the examples in the manual? This is all pretty straight forward stuff.

 

http://php.net/oop5

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.