classes - beginner question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcmcom
Forum Newbie
Posts: 14
Joined: Thu Jan 31, 2008 7:23 am

classes - beginner question

Post 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
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Re: classes - beginner question

Post 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;
} 
 
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: classes - beginner question

Post 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;
  }
}
 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: classes - beginner question

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply