Image copyright Reuters Image caption Demonstrators against SB4 gathered outside a court hearing in June

A US federal judge has halted a new Texan immigration law on "sanctuary cities" just two days before it is scheduled to go into effect.

The bill aimed to force local law enforcement to obey requests from immigration officers to hold illegal immigrants for deportation.

"Sanctuary cities" came under fire from Donald Trump in an executive order during his first week as president.

The Texan law was due to come into effect on Friday.

But on Wednesday, a federal judge ruled it was unlikely to withstand constitutional tests, and prevented implementation of key parts of the bill, called SB4.

In a 94-page decision, US District Judge Orlando Garcia commented: "There is overwhelming evidence by local officials, including local law enforcement, that SB4 will erode public trust and make many communities and neighborhoods less safe."

"There is also ample evidence that localities will suffer adverse economic consequences which, in turn, harm the State of Texas."

He said the court could not "second guess the legislature" but the state could not violate the constitution.

What is a 'sanctuary city'?

The term applies to cities in the US that have policies to limit the assistance given to federal immigration authorities

Officials in those areas, including local law enforcement, are not allowed to enquire as to an individual's immigration status in the course of their duties

It is not a legal term so the way it is implemented can vary, but the policies can be set in law or just part of local policing practices

The idea got traction in the 1980s after Los Angeles told its police force to stop questioning people solely to determine their immigration status, in a bid to encourage immigrant communities to work with police

More than 400 jurisdictions across the US, including New York, San Francisco, Boston, Chicago and Seattle, have enacted policies protecting undocumented immigrants

Critics say these policies undermine federal law enforcement efforts and encourage illegal immigration

More about 'sanctuary cities'

Life as refugees in US 'sanctuary cities'

Texas Governor Greg Abbott said the decision made "Texas' communities less safe".

He said he would immediately appeal the decision and was confident the law would eventually be upheld.

Mr Trump's executive order in January covered a range of immigration issues but specifically targeted the 400 or so jurisdictions with policies protecting undocumented immigrants - nicknamed "sanctuary cities".

The presidential order authorised the federal government to withhold funding from cities with such policies.

The Texan bill, passed by the state legislature earlier this year, was set to be one of the first laws of its kind since Mr Trump's order.