Page 1 of 1

classes - beginner question

Posted: Thu Jan 31, 2008 7:26 am
by mcmcom
hi all,
im a first time php programmer however long time C# / javascript / vb.net / etc programmer so i am fully versed in oop and know how to program (theory, etc).

I am charged with making a rather simple search class in php that uses XPath. I just have some basic questions about classes in php before i get too far along.

1. Are classes in php similar to C# where i can have the class in a separate physical file and include it in another php file, then instantiate it and call its methods just like :

include myclass.php

FTSearch mySearch = new FTSearch() // construct it
var $myArray = mySearch.PerformSearch(blah) //activate a method that returns a string or array

2. also how do i construct a method that returns a value? in C# it would be like :
public string ThisReturnsAString(){
return myString;
}

thats it for now.
thanks in advance,
mcm

Re: classes - beginner question

Posted: Thu Jan 31, 2008 7:59 am
by webspider
also how do i construct a method that returns a value? in C# it would be like :
public string ThisReturnsAString(){
return myString;
}
in php return statement inside the function is used to return a value.return statement causes the function to end its execution immediately and pass control back to the line from which it was called. here modifier public and return datatype string is not used

Code: Select all

 
 
function ThisReturnsAString()
{
  return $myString;
} 
 

Re: classes - beginner question

Posted: Thu Jan 31, 2008 8:38 am
by Zoxive
webspider wrote:here modifier public and return datatype string is not used
They are used in PHP5 Classes, which I believe the OP was asking.

Code: Select all

<?php
class Test_Class{
 
  private $Val = null;
 
  public $Val2 = null;
 
  public function getVal(){
    return $this->Val;
  }
  public function setVal($Value){
    $this->Val = $Value;
    return true;
  }
  // Static
  static public function _getVal(){
    return self::$Val;
  }
  static public function _setVal($Value){
    self::$Val = $Value;
    return true;
  }
}
 

Re: classes - beginner question

Posted: Thu Jan 31, 2008 9:44 am
by pickle
mcmcom wrote: include myclass.php

FTSearch mySearch = new FTSearch() // construct it
var $myArray = mySearch.PerformSearch(blah) //activate a method that returns a string or array
Almost:

Code: Select all

include 'myclass.php';
$mySearch = new FTSearch();
$myArray = $mySearch->PerformSearch('blah');
Also, please use [ php ] tags when posting code - as you can see it highlights a lot nicer.